简体   繁体   English

rails 4,collection_select,将多个属性传递到表单提交的params哈希中?

[英]rails 4, collection_select, pass more than one attribute into params hash on form submit?

I'm using collection_select to select an option from a drop down. 我正在使用collection_select从下拉列表中选择一个选项。 When I submit the form I want to send multiple params into the params hash. 当我提交表单时,我想将多个参数发送到params哈希中。 In this case 'team_id' and 'team_name'. 在这种情况下,“ team_id”和“ team_name”。 team_id is showing up in the params hash just fine. team_id很好地显示在params哈希中。 'team_name' is the value which shows up in the drop down list itself. “ team_name”是显示在下拉列表本身中的值。

view.html.erb 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 hash 参数哈希

{"utf8"=>"✓", "authenticity_token"=>"bdazhLNLZ0QunrpJT7Gu63ipX76WME+ENSxL/B0XGeFL/GP5nishozmQENe22aelfcnnhnPBr4B35MeRL+kJLQ==", "carpool"=>{"team_id"=>"1923565"}, "commit"=>"Create Carpool", "controller"=>"carpools", "action"=>"create"}

How can I pass team_name into the params hash? 如何将team_name传递给params哈希?

You can get what you want without jumping through flaming hoops by just creating an instance of Team (which you are probably already doing anyhow) and just getting the name directly. 只需创建一个Team实例(无论如何,您可能已经在做)并直接获取名称,您就可以得到想要的内容而不必费时费力。

In your controller method: 在您的控制器方法中:

@team = Team.find(params[:team_id])
@team_name = @team.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM