从DOM获取数据以通过link_to传递给rails控制器

时间:2013-09-26 23:10:59

标签: javascript jquery ruby-on-rails

我的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字段的值吗?

谢谢!

1 个答案:

答案 0 :(得分: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])