Rails 5 form_for with checkbox array

生来就可爱ヽ(ⅴ<●) 提交于 2020-08-25 07:28:45

问题


I am using a PostgreSQL database and Rails 5.0.6. I try to build a course allocation WebApp for the school where I am working. For each course the teachers are able to select which forms are allowed to visit the course.

Migration file:

  def up
    create_table :courses do |t|
      t.integer :number, null: false
      t.string :name, null: false
      t.text :description, null: false
      t.decimal :level, array: true, default: []
      t.integer :max_visitor, null: false
      t.integer :min_visitor
      t.integer :pos_visit
      t.timestamps
    end
  end

In my Controller:

    params.require(:course).permit(:number, :name, :description, :level [], :max_visitor, :min_visitor, :pos_visits)

I already read this post: Rails 5 strong params with an Array within check boxes values. But the Syntax params.require(:product).permit(:id, **category_ids: []**) doesnt work for me even though I am using rails 5 as well. I am not sure if :level [] really works but it seems to be correct syntax.

This is my Form:

<%= form_for @course do |t| %>
            <%= t.text_field :number, class: 'form-control' %>
            <%= t.text_field :name, class: 'form-control' %>
            <%= t.text_area :description, class: 'form-control' %>
            <%= t.check_box :level[], 1%>
            <%= t.check_box :level[], 2%>
            <%= t.check_box :level[], 3%>
            <%= t.check_box :level[], 4%>
            <%= t.text_field :max_visitor, class: 'form-control' %>
            <%= t.text_field :min_visitor, class: 'form-control' %>
            <%= t.text_field :pos_visit, class: 'form-control' %><br/>
        <%= t.submit "bestätigen", class: "btn btn-success"%>
<% end %>

This check_box syntax seems to be wrong. May anyone help me with the right syntax of the check_boxes? Thanks for help.


回答1:


There's a collection_check_boxes helper method for this:

<%= form_for @course do |f| %>
  <%= f.collection_check_boxes(:level, { 'One': 1, 'Two': 2, 'Three': 3 }, :last, :first) %>
<% end %>

The third argument is the method used to get the value from the "collection", and the fourth is the method used to get the label from the "collection". This helper method automatically converts the Hash into an array, that's why I'm using last and first here.

It's also possible to style it the way you want e.g. using Bootstrap:

<%= f.collection_check_boxes(:level, { 'One': 1, 'Two': 2, 'Three': 3 }, :last, :first) do |b| %>
  <div class="form-check form-check-inline">
    <%= b.check_box class: 'form-check-input' %>
    <%= b.label class: 'form-check-label' %>
  </div>
<% end %>



回答2:


<%= check_box_tag 'level[]', 1%>
<%= check_box_tag 'level[]', 2%>
<%= check_box_tag 'level[]', 3%>
<%= check_box_tag 'level[]', 4%>

But when you use check_box_tags in form_for, then the parameters level[], will be outside off the strong parameters array you usually use in the controller#new function.

Parameters: {"course"=>{"number"=>"12", "name"=>"tanzen", "description"=>"efwefggw", "max_visitor"=>"12", "min_visitor"=>"5", "pos_visit"=>"2"}, "level"=>["1", "3", "4"], "commit"=>"bestätigen"}

So I added the level manually

@course = Course.new(course_params)
@course.level = params[:level]


来源:https://stackoverflow.com/questions/48231445/rails-5-form-for-with-checkbox-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!