LoginSignup
2
1

More than 3 years have passed since last update.

Rails ラジオボタンの複数情報を保存する方法

Last updated at Posted at 2020-12-06

Rails ラジオボタンの複数情報を保存する方法 

環境

Ruby:2.6.5
Rails:6.0.3.


記述理由

探せど探し方もわからず2日間彷徨ったため、忘備録、戒めも踏まえて記述する
基本的な知識を踏まえて、今後の理解に務める。

こうした方がいいなどのご指摘があり、教えていいよという稀有な方がございましたら、なんなりとお願いします。


①HTML側:複数チェックの情報を格納して送付する記述

index.html.erb
まず form_withの記述の方法

<%= form_with(model: @company_type,url: companies_types_path(@company_type),
 method: :post,local: true) do |f| %>
 
中略 繰り返し表示させるための記述の方法
 
 <div class="tab-pane fade show active" id="list-doboku" role="tabpanel"
 aria-labelledby="list-doboku-list">
   <% industry_type = ["足場工事","舗装工事","しゆんせつ工事","石工事","造園工事","土木工事"] %>
    <% industry_type.each do |q| %><br>
      <%= f.check_box :industry_type , {multiple: true},q,nil %>
      <%= f.label :industry_type, q %>
    <% end %>
 </div>

中略 form_withを送付するための方法

     <div class="text-center mb-5 col-6">
       <%= f.submit "記録する" ,class:"btn btn-block send-button tx-tfm" %>
     </div>
   <% end %> 

①-2:HTML側:このように表示することができました

b8853ff8c03b14513b8abe3e4c1d5a0f.png


②controller側:複数チェックの情報を受け取り保存する記述

モデルs.controller.rb
def create
 @company_type = CompanyType.new(company_type_params)
   if @company_type.valid?
      @company_type.save
      redirect_to companies_types_path
   else
      render :index
   end
end

private

def company_type_params
  params.require(:company_type).permit(:industry_type =>[]).merge(company_detail_id: current_user.id)
end

②-2:DB側:このように保存することができました

938234ae1a61749fa1679e5891ad77b2.png


反省

探し方も悪かったと思いますが、つまづく人が少ないから探せなかったのかもしれません。
Qiita記事も初投稿、まだまだ勉強することはあると自分を勇気づけて次のエラーに取り掛かります。
圧倒的知識不足による問題の気がしますが、書いてみないことには分からないので投稿しました。

2
1
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
2
1