如何使用带有 has_many 的 Rails 4 强参数:通过关联? [英] How to use Rails 4 strong parameters with has_many :through association?

查看:16
本文介绍了如何使用带有 has_many 的 Rails 4 强参数:通过关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取 has_many 时遇到了麻烦:通过与 Rails 4 的强参数关联工作.我有一个名为 Checkout 的模型,我需要从新结帐表单中的 Employee 模型中选择一个人.结帐和员工通过 Employment 模型相关联.

当我尝试创建新的结帐时遇到此错误:

NoMethodError in CheckoutsController#create#<Checkout:0x007ff4f8d07f88>的未定义方法`employee'

我的创建操作、结帐参数或新结帐表单似乎有问题.这是创建操作:

 def 创建@user = current_user@checkout = @user.checkouts.build(checkout_params)response_to do |格式|如果@checkout.saveformat.html { redirect_to @checkout,注意:'结帐已成功创建.'}别的format.html { 渲染动作:'新' }结尾结尾结尾

我的结帐参数:

def checkout_paramsparams.require(:checkout).permit(:job, :employee_ids, :shift, :date, :hours, :sales, :tips, :owed, :collected, :notes)结尾

我的新结帐表格:

<%= f.label :employee %><br><%= f.collection_select(:employee_ids, Employee.all.collect, :id, :full_name, {:prompt => "请选择"}) %>

但我无法弄清楚 Rails 4 和强参数发生了什么变化.在 Rails 3 中,这种类型的关联和表单使用 attr_accessible 而不是 strong_parameters 对我有用.

相关文件

错误的完整跟踪:https://gist.github.com/leemcalilly/0cb9e2b539f9e1925a3d

模型/checkout.rb:https://gist.github.com/leemcalilly/012d6eae6b207beb147a

控制器/checkouts_controller.rb:https://gist.github.com/leemcalilly/a47466504b7783b31773

views/checkouts/_form.html.erbhttps://gist.github.com/leemcalilly/ce0b4049b23e3d431f55

模型/员工.rb:https://gist.github.com/leemcalilly/46150bee3e6216fa29d1

controllers/employees_controller.rb:https://gist.github.com/leemcalilly/04f3acdac0c9a678bca8

模型/就业.rb:https://gist.github.com/leemcalilly/6adad966dd48cb9d1b39

db/schema.rb:https://gist.github.com/leemcalilly/36be318c677bad75b211

解决方案

请记住,您为强参数(employees、employee_ids 等)指定的名称在很大程度上是无关紧要的,因为它取决于名称​​您 选择提交.基于命名约定,强参数没有魔法"作用.

原因 https://gist.github.com/leemcalilly/a71981da605187d46d96 抛出未经允许的参数"错误在 'employee_ids' 上是因为它需要一个 array 标量值,根据 https://github.com/rails/strong_parameters#nested-parameters,不仅仅是一个标量值.

# 如果不是:... "employee_ids" =>1"...# 你有过:... "employee_ids" =>[1"]

然后你的强参数会起作用,特别是:

... { :employee_ids =>[] } ...

因为它接收的是一组标量值,而不仅仅是一个标量值.

I'm having trouble getting a has_many :through association working with Rails 4's strong parameters. I have a model called Checkout and I need to select a person from the Employee model in the new checkout form. Checkouts and Employees are associated through an Employment model.

I'm getting this error when I try to create a new checkout:

NoMethodError in CheckoutsController#create
undefined method `employee' for #<Checkout:0x007ff4f8d07f88>

It seems that there's something wrong with either my create action, my checkout parameters or my new checkout form. Here's the create action:

  def create    
    @user = current_user
    @checkout = @user.checkouts.build(checkout_params)

    respond_to do |format|
      if @checkout.save
        format.html { redirect_to @checkout, notice: 'Checkout was successfully created.' }
      else
        format.html { render action: 'new' }
      end
    end
  end

My checkout params:

def checkout_params
      params.require(:checkout).permit(:job, :employee_ids, :shift, :date, :hours, :sales, :tips, :owed, :collected, :notes)
end

My new checkout form:

<div class="field">
     <%= f.label :employee %><br>
     <%= f.collection_select(:employee_ids, Employee.all.collect, :id, :full_name, {:prompt => "Please select"} ) %>
</div>

But I can't figure out what has changed with Rails 4 and strong parameters. In Rails 3 this type of association and form worked for me using attr_accessible instead of strong_parameters.

Relevant Files

Full Trace of the error: https://gist.github.com/leemcalilly/0cb9e2b539f9e1925a3d

models/checkout.rb: https://gist.github.com/leemcalilly/012d6eae6b207beb147a

controllers/checkouts_controller.rb: https://gist.github.com/leemcalilly/a47466504b7783b31773

views/checkouts/_form.html.erb https://gist.github.com/leemcalilly/ce0b4049b23e3d431f55

models/employee.rb: https://gist.github.com/leemcalilly/46150bee3e6216fa29d1

controllers/employees_controller.rb: https://gist.github.com/leemcalilly/04f3acdac0c9a678bca8

models/employment.rb: https://gist.github.com/leemcalilly/6adad966dd48cb9d1b39

db/schema.rb: https://gist.github.com/leemcalilly/36be318c677bad75b211

解决方案

Keep in mind that the name you give to your strong parameters (employees, employee_ids, etc.) is largely irrelevant because it depends on the name you choose to submit. Strong parameters work no "magic" based upon naming conventions.

The reason https://gist.github.com/leemcalilly/a71981da605187d46d96 is throwing an "Unpermitted parameter" error on 'employee_ids' is because it is expecting an array of scalar values, per https://github.com/rails/strong_parameters#nested-parameters, not just a scalar value.

# If instead of:
... "employee_ids" => "1" ...
# You had:
... "employee_ids" => ["1"]

Then your strong parameters would work, specifically:

... { :employee_ids => [] } ...

Because it is receiving an array of scalar values instead of just a scalar value.

这篇关于如何使用带有 has_many 的 Rails 4 强参数:通过关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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