Ruby on rails 具有多态关联的质量分配错误

Ruby on rails 具有多态关联的质量分配错误,ruby-on-rails,model,associations,polymorphic-associations,mass-assignment,Ruby On Rails,Model,Associations,Polymorphic Associations,Mass Assignment,提交has_one多态模型的嵌套表单时,我遇到了一个批量分配错误。表单正在尝试基于创建员工和图片实例 我将非常感谢任何一个用于has_one多态模型的嵌套创建表单的功能示例!我知道有很多关于批量分配错误的问题,但我从来没有见过一个多态关联的工作示例 模型 控制器/员工\u controller.rb 错误 在模型中,我尝试了:插图、:图片、:插图属性、:插图属性、:插图属性、:图片属性、:图片属性等的所有排列。有什么提示或示例吗 编辑: _form.html.erb 您需要适当地指定嵌套的_属性

提交has_one多态模型的嵌套表单时,我遇到了一个批量分配错误。表单正在尝试基于创建员工和图片实例

我将非常感谢任何一个用于has_one多态模型的嵌套创建表单的功能示例!我知道有很多关于批量分配错误的问题,但我从来没有见过一个多态关联的工作示例

模型

控制器/员工\u controller.rb

错误

在模型中,我尝试了:插图、:图片、:插图属性、:插图属性、:插图属性、:图片属性、:图片属性等的所有排列。有什么提示或示例吗

编辑:

_form.html.erb


您需要适当地指定嵌套的_属性。接受\u嵌套的\u属性\u以关联的名称作为参数,然后需要将相同的assoc\u属性添加到attr\u accessible。因此,将您的员工模型更改为

class Employee < ActiveRecord::Base
  has_one :picture, :as => :illustrated
  accepts_nested_attributes_for :picture
  attr_accessible :name, :picture_attribute
end

您可以添加视图文件代码吗?它应该是:图解的属性。另一件事是在EmployeesNewAction中将@employee.picture=picture.new替换为@employee.build\u picture。这是使用has_one/belish_to association生成关联记录的正确方法。谢谢-添加了查看代码。我尝试了:在模型中演示了_属性,但得到了相同的错误。也更改为@employee.build\u picture,但仍然得到相同的错误。我知道了!。它应该是模型和视图中的picture_属性。试试看,应该行得通。那完全行得通!Freakin':as=>及其技巧。你想添加一个答案,这样我就可以给你分数,还是这样?
create_table :pictures do |t|
    t.string :filename
    t.references :illustrated, polymorphic: true
end

create_table :employees do |t|
    t.string :name
end
...
def new
    @employee = Employee.new
    @employee.picture = Picture.new
end

def create
    @employee = Employee.new(params[:employee])
    @employee.save
end
...
Can't mass-assign protected attributes: illustrated

app/controllers/employees_controller.rb:44:in `create'

{"utf8"=>"✓", "authenticity_token"=>"blah"
 "employee"=>{"illustrated"=>{"filename"=>"johndoe.jpg"},
 "name"=>"John Doe"},
 "commit"=>"Create Employee"}
<%= form_for(@employee) do |f| %>

  <%= f.fields_for :illustrated do |form| %>
    <%= form.text_field :filename %>
  <% end %>

    <%= f.text_field :name %>
    <%= f.submit %>

<% end %>
class Employee < ActiveRecord::Base
  has_one :picture, :as => :illustrated
  accepts_nested_attributes_for :picture
  attr_accessible :name, :picture_attribute
end
<%= f.fields_for :picture do |form| %>