Rails 4中的ActiveRecord :: AssociationTypeMismatch

Mik*_*kie 2 ruby-on-rails-4

我已经嵌套在我的表格属性和2款 - 艺术品和ArtDescription,其连接HAS_ONE/belongs_to的,但我有错误

ArtworksController中的ActiveRecord :: AssociationTypeMismatch#创建ArtDescription(#30842620),获得ActionController :: Parameters(#9409680)

怎么了?

形成

  <%= form_for :artwork, url: artworks_path do |f| %>
    <p>
        name:<%= f.text_field :name %></br>
        author:<%= f.text_field :author %></br>
        date:<%= f.text_field :date %></br>
        museum:<%= f.text_field :museum %></br>
        place:<%= f.text_field :create_place %></br>
        comment:<%= f.text_field :comment %></br>
    </p>
    <p>
    <%= f.fields_for :art_description do  |artdesc|%>
        type:<%=  artdesc.text_field  :type  %></br>
        base:<%=  artdesc.text_field  :base  %></br>
        style:<%=  artdesc.text_field  :style  %></br>
        genre:<%=  artdesc.text_field  :genre  %></br>
        plot:<%=  artdesc.text_field  :plot  %></br>
        reference: <%=  artdesc.text_field  :reference  %></br>
        format_weight: <%=  artdesc.text_field  :format_weight%></br>
        format_height:<%=  artdesc.text_field  :format_height  %></br>
         <% end %>
       <%= f.submit ('Ok')%> </p>
    <% end %>
Run Code Online (Sandbox Code Playgroud)

控制器ArtworksController

def create
@artwork = Artwork.create(artwork_params)
if @artwork.save
       redirect_to artwork_path(@artwork)
    else
       render 'new'
    end
end
Run Code Online (Sandbox Code Playgroud)

模型

class Artwork < ActiveRecord::Base

 has_one :art_description
 accepts_nested_attributes_for  :art_description
end
Run Code Online (Sandbox Code Playgroud)

强参数

def artwork_params
params.require(:artwork).permit(:name, :author, :date, :museum, :comment, :create_place,   art_description: [ :type, :base, :style, :genre, :plot, :reference, :format_weight, :format_height])
end
Run Code Online (Sandbox Code Playgroud)

Dyl*_*kow 5

你强大的params线应该使用art_description_attributes,而不是art_description:

params.require(:artwork).permit(:name, :autor, :date, :museum, :comment, :create_place,      art_description_attributes: [ :type, :base, :style, :genre, :plot, :reference, :format_weight, :format_height])
Run Code Online (Sandbox Code Playgroud)