开发者

Rails 3 form only returning last element

开发者 https://www.devze.com 2023-02-25 03:30 出处:网络
I\'m trying to build a form that will list all users and allow you to check the ones that you want to add to a team. Here\'s my first cut at the form:

I'm trying to build a form that will list all users and allow you to check the ones that you want to add to a team. Here's my first cut at the form:

<div id="add_team_mates">
  <%= form_tag do %>
  <%= will_paginate @users %>
  <ul class="users">
    <% @users.each do |user| %>
    <li>
      <%= gravatar_for user, :size => 30 %>
      <%= link_to user.name, user %>
      <%= check_box_tag("add", user.id) %>
    </li>
    <% end %>
  </ul>
  <%= submit_tag "Add Team Mates", :action => "add_team_mates" %>
  <% end %>
</div>

And, right now this is all that I have in the contro开发者_开发知识库ller:

def add_team_mates

end

The problem is that if I check multiple users, I only get the last user.id rather than multiple is as I'd expect. Here's some example from the log:

Started POST "/teams/5" for 127.0.0.1 at 2011-04-14 15:28:13 -0700
  Processing by TeamsController#add_team_mates as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"IHFDevfKES8NibbCMlRa1t9qHn4/ZMKalK1Kjczh2gM=", "add"=>"3", "commit"=>"Add Team Mates", "id"=>"5"}
Completed   in 12ms

Any help on this would be greatly appreciated. Thanks!


All your checkboxes have the same name, change the line to check_box_tag("add[]",user.id)

In the controller your parameters will be like so:

params[:add] = ['foo','bar','baz']

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号