LoginSignup
0
0

More than 3 years have passed since last update.

【Rails5】check_boxで任意の文字列を渡す方法(ガチ初学者向け)

Last updated at Posted at 2020-11-20

初投稿です。
初学者のため、参考程度に見ていただければ幸いです。

至らない点、わかりにくい点などあればコメントで教えていただければ幸いです。

version

Ruby 2.5.7
Rails 5.2.4.4

How to

まずチェックボックスフォーム機能を実装する際に、デファルトだとか機能ようなコードになると思います。(form_withを使う場合)

<%= form_with model: 変数, url:遷移先のパス do |f| %>
   <%= f.check_box :カラム名,{オプション(クラス名など} %> ←チェックボックスを表示
   <%= f.submit %>
<% end %> 

このようにするとデフォルトで"1"か"0"が送られます。

<%= form_with model: 変数, url:遷移先のパス do |f| %>
   <%= f.check_box :カラム名, {オプション(クラス名など}, "true","fales" %> ←チェックボックスを表示
   <%= f.submit %>
<% end %> 

このように記述すればtrue,falesを渡すことができます。

<%= form_with model: 変数, url:遷移先のパス do |f| %>
   <%= f.check_box :カラム名, {オプション(クラス名など}, "attende","absence" %> ←チェックボックスを表示
   <%= f.submit %>
<% end %> 

同様にtrue,falesの部分を書き換えれば、任意の値を渡すことができます。

ポイントは、オプションの後ろに記述することです。
渡したい文字列の後ろにクラス名などのオプションを指定するとエラーが出ます。

簡単なようで意外と車学者がつまずくポイントかなと思い記事にさせていただきました。

誰かの参考になれば・・・

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0