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

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

问题描述

我无法得到一个的has_many:通过联合使用Rails工作4强大的参数。我有一个名为结帐模式,我需要选择一个人从员工模式,在新的结算形式。检出,并且员工通过一个工作模型相关联。

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

我结账PARAMS:

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>

不过,我想不出有什么使用Rails 4和强大的参数改变。在Rails 3这种类型的关联和形式的工作,而不是使用strong_parameters我attr_accessible。

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.

相关文件

错误的完整曲线: <一href="https://gist.github.com/leemcalilly/0cb9e2b539f9e1925a3d">https://gist.github.com/leemcalilly/0cb9e2b539f9e1925a3d

型号/ checkout.rb: <一href="https://gist.github.com/leemcalilly/012d6eae6b207beb147a">https://gist.github.com/leemcalilly/012d6eae6b207beb147a

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

控制器/ checkouts_controller.rb: <一href="https://gist.github.com/leemcalilly/a47466504b7783b31773">https://gist.github.com/leemcalilly/a47466504b7783b31773

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

意见/签/ _form.html.erb <一href="https://gist.github.com/leemcalilly/ce0b4049b23e3d431f55">https://gist.github.com/leemcalilly/ce0b4049b23e3d431f55

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

型号/ employee.rb: <一href="https://gist.github.com/leemcalilly/46150bee3e6216fa29d1">https://gist.github.com/leemcalilly/46150bee3e6216fa29d1

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

控制器/ employees_controller.rb: <一href="https://gist.github.com/leemcalilly/04f3acdac0c9a678bca8">https://gist.github.com/leemcalilly/04f3acdac0c9a678bca8

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

型号/ employment.rb: <一href="https://gist.github.com/leemcalilly/6adad966dd48cb9d1b39">https://gist.github.com/leemcalilly/6adad966dd48cb9d1b39

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

DB / schema.rb: <一href="https://gist.github.com/leemcalilly/36be318c677bad75b211">https://gist.github.com/leemcalilly/36be318c677bad75b211

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

推荐答案

请记住,你给你的强大的参数(员工,employee_ids等)的名称在很大程度上是不相关的,因为它依赖于名称的的选择提交。强大的参数的工作原理基于命名约定没有魔法。

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.

原因<一href="https://gist.github.com/leemcalilly/a71981da605187d46d96">https://gist.github.com/leemcalilly/a71981da605187d46d96抛出一个未经许可的参数错误employee_ids,是因为它期待一个阵列标值的,每<一href="https://github.com/rails/strong_parameters#nested-parameters">https://github.com/rails/strong_parameters#nested-parameters,不只是一个标量值。

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.

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

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