Mikkie
Mikkie

Reputation: 33

ActiveRecord::AssociationTypeMismatch in Rails 4

I have nested attributes in my form and 2 models - Artwork and ArtDescription, which connected has_one/belongs_to but i've got error

ActiveRecord::AssociationTypeMismatch in ArtworksController#create ArtDescription(#30842620) expected, got ActionController::Parameters(#9409680)

whats wrong?

Form

  <%= 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 %>

Controller ArtworksController

def create
@artwork = Artwork.create(artwork_params)
if @artwork.save
       redirect_to artwork_path(@artwork)
    else
       render 'new'
    end
end

Model

class Artwork < ActiveRecord::Base

 has_one :art_description
 accepts_nested_attributes_for  :art_description
end

Strong Parametrs

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

Upvotes: 2

Views: 3254

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124429

Your strong params line should use art_description_attributes, not 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])

Upvotes: 5

Related Questions