0

我的 Rails 应用程序中有以下视图:

<div class="row">
  <div class="span11 center searchFieldContainer">
    <div class="span3">
        <%= collection_select :matchup, :player_1, Player.all, :id, :last_name, {}, { :multiple => false, class: "matchupSearchField", id: "player_1_search_field" } %>
    </div>
    <div class="span3">
        <h2>VS</h2>
      </div>
      <div class="span3">
        <%= collection_select :matchup, :player_2, Player.all, :id, :last_name, {}, { :multiple => false, class: "matchupSearchField", id: "player_2_search_field" } %>
    </div>
  </div>

  <%= link_to "View Matchup", 
    findMatchup_path(:player_1 => **ID_OF_PLAYER_1**, :player_2 => **ID_OF_PLAYER_1**),
    :method => :get, class: "my_class" %>
</div>

这是'/home.html.erb'的一部分,我的root_path。

如何将 link_to 中的ID_OF_PLAYER_1替换为上面 collection_select 中选择的玩家的 ID?我可以使用 jQuery 来获取该 collection_select 字段的值吗?

谢谢!

4

1 回答 1

0
<%= form_tag('/findMatchup', method: :get) do %>
    <div class="row">
    <div class="span11 center searchFieldContainer">
        <div class="span3">
            <%= collection_select :matchup, :player_1, Player.all, :id, :last_name, {}, { :multiple => false, class: "matchupSearchField", id: "player_1_search_field" } %>
        </div>
        <div class="span3">
            <h2>VS</h2>
          </div>
          <div class="span3">
            <%= collection_select :matchup, :player_2, Player.all, :id, :last_name, {}, { :multiple => false, class: "matchupSearchField", id: "player_2_search_field" } %>
        </div>
      </div>
    </div>
  <div class="row">
    <%= submit_tag "View Matchup", class: "btn" %>
  </div>
<% end %>

控制器使用以下方式访问变量:

@player1 = Player.find(params[:matchup][:player_1])
于 2013-09-27T00:03:29.407 回答