Rails中的复选框
我第一次尝试使用Multiple选项:
#views/values/new.html.erb*
<% @values =["Power", "Independance", "Tradition"] %>
<%= form_for @resultvalue do |f| %>
<% @values.each do | value | %>
<%= f.check_box :values, { multiple: true }, value, false %>
<%= f.label value %>
<% end %>
<%= f.submit%>
<% end %>
#controller/resultvalue_controller.rb
class ResultvaluesController < ApplicationController
[...]
def resultvalue_params
params.require(:resultvalue).permit(values:[])
end
end
#models/resultvalue.rb
class Resultvalue < ApplicationRecord
serialize :values, Array
end
这是我提交时的日志:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"lf/sH123AoHdGyA==", "resultvalue"=>{"values"=>["Power", "Tradition"]}, "commit"=>"create", "id"=>"1"}
我遵循了此教程-> https://medium.com/programming-problem-solving-logbook/rails-multiple-checkboxes-e9c4c7fda356