rails 3 has_many :通过带有复选框的表单 [英] rails 3 has_many :through Form with checkboxes

查看:38
本文介绍了rails 3 has_many :通过带有复选框的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前问过类似问题.

Rails 3 has_many :through 表单

但是无法从用户表单中获取与要创建的雇佣关系的关系.

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

已阅读http://www.justinball.com/2008/07/03/checkbox-list-in-ruby-on-rails-using-habtm/http://millarian.com/programming/ruby-on-rails/quick-tip-has_many-through-checkboxes/(我真的希望它有效.)

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 record 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 %>

推荐答案

我可能错了,但我认为 check_box_tag 函数的第一个 arg 是输入的实际名称,所以代替

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]'

你可以尝试类似的东西

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

让我知道它是否有效!

这篇关于rails 3 has_many :通过带有复选框的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆