[英]No more than one vaule being passed from a form in rails from multiple collection_select boxes
[英]rails 4, collection_select, pass more than one attribute into params hash on form submit?
我正在使用collection_select从下拉列表中选择一个选项。 当我提交表单时,我想将多个参数发送到params哈希中。 在这种情况下,“ team_id”和“ team_name”。 team_id很好地显示在params哈希中。 “ team_name”是显示在下拉列表本身中的值。
view.html.erb
<%= form_for @carpool do |f| %>
<h3>Select Team</h3>
<div class="form-group">
<%= f.collection_select :team_id, @ts_teams ? @ts_teams : [], :id, :name, include_blank: true %>
</div>
<%= f.submit 'Create Carpool', :class => 'button left' %>
<% end %>
参数哈希
{"utf8"=>"✓", "authenticity_token"=>"bdazhLNLZ0QunrpJT7Gu63ipX76WME+ENSxL/B0XGeFL/GP5nishozmQENe22aelfcnnhnPBr4B35MeRL+kJLQ==", "carpool"=>{"team_id"=>"1923565"}, "commit"=>"Create Carpool", "controller"=>"carpools", "action"=>"create"}
如何将team_name传递给params哈希?
只需创建一个Team实例(无论如何,您可能已经在做)并直接获取名称,您就可以得到想要的内容而不必费时费力。
在您的控制器方法中:
@team = Team.find(params[:team_id])
@team_name = @team.name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.