我使用collection_select从下拉列表中选择一个选项。当我提交表单时,我想将多个params发送到params哈希中。在本例中为"team_id"one_answers"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 %>
params散列
{"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