Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Has many through Rails 4:带有“已通过”的复选框_Has Many Through_Ruby On Rails 4 - Fatal编程技术网

Has many through Rails 4:带有“已通过”的复选框

Has many through Rails 4:带有“已通过”的复选框,has-many-through,ruby-on-rails-4,Has Many Through,Ruby On Rails 4,我正在构建一个应用程序,它必须将一个任务分配给多个雇主 我构建了以下模型: #assignment.rb class Assignment < ActiveRecord::Base has_many :employer_assignments has_many :employers, :through => :employer_assignments end #employer.rb class Employer < ActiveRecord::Base

我正在构建一个应用程序,它必须将一个任务分配给多个雇主

我构建了以下模型:

#assignment.rb
class Assignment < ActiveRecord::Base
    has_many :employer_assignments
    has_many :employers, :through => :employer_assignments
end

#employer.rb
class Employer < ActiveRecord::Base
    has_many :employer_assignments
    has_many :assignments, :through => :employer_assignments
end

#employer_assignment.rb
class EmployerAssignment < ActiveRecord::Base
    belongs_to :employer
    belongs_to :assignment
end
#assignment.rb
类赋值:雇主
结束
#雇主.rb
类:雇主
结束
#雇主分配.rb
类EmployerSignement
现在我想将表单保存到雇主分配表中,但我在表单中使用的以下代码不起作用

<div class="field">
    <%= f.label :employer_ids %><br />
    <%= collection_check_boxes(:assignment, :employer_ids, Employer.all, :id, :name) %>
</div>


我确实向我的分配控制器添加了:雇主ID,我试图从中发送表单,该表单创建了分配,但没有在雇主分配表中创建记录。
当我通过控制台(Assignment.last.employers添加它们时,由于rails4中的强参数(@emil kampp提到了这一点),您可能会在日志中获得一个
未经许可的参数:
),在新的rails生成之后,它们会在您的控制器中生成。因此,使用您的代码,看起来像:

class EmployersController < ApplicationController
  # <snip>
  def update
    @employer.update(employer_params)
  end

  def employer_params
    params.require(:employer).permit(:name, { :employer_ids => [] })
  end
end
class EmployersController[]})
结束
结束

也可以在上面看到这个答案。希望这可以节省一些周期。

你找到解决方案了吗?我有一个类似的问题。不保存到DBIIRC rails 4默认使用强参数,它不支持数组类型。因此,你需要显式定义
雇主ID
应该是一个数组,例如那是允许通过的。