开发者

rails 3 has_many :through Form with checkboxes

Asked similar before.

Rails 3 has_many :through Form

But can't get the relationship with employmentships to be created from the users form.

Have read http://www.justinball.com/2008/07/03/checkbox-list-in-ruby-on-rails-using-habtm/ and http://millarian.com/programming/ruby-on-rails/quick-tip-has_many-through-checkboxes/ (which I was really hoping that it worked.)

Form submits, but only creates a blank recor开发者_开发问答d in employmentship.

<%= form_for @user do |f| %>
...
<p>
   <% Company.all.each do |company| %>
        <%= check_box_tag :company_ids, company.id, @user.companies.include?(company), :name => 'user[company_ids][]' -%>
     <%= label_tag :companies_ids, company.id %>
   <% end %>
</p>
<p><%= f.submit %></p>
<% end %>


Include a hidden field tag in the form to make sure something gets submitted when none of the check boxes are selected. This should work, after the <%end%>:

<%= hidden_field_tag "user[company_ids][]" %>


I may be wrong, but I think that the first arg of the check_box_tag function is the actual name of the input, so instead of

check_box_tag :company_ids, company.id, @user.companies.include?(company), :name => 'user[company_ids]'

you could try something like

check_box_tag 'user[company_ids]', company.id, @user.company_ids.include?(company.id)

Let me know if it works!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜