ruby on rails - strong parameters doesn't allow to create nested attributes with json. ActiveRecord::AssociationTypeMismatch error -
i have 2 models: conference , talks. conference has_many talks ,
accepts_nested_attributes_for :talks, :allow_destroy => true
params posting looks that:
{"id"=>"2", "name"=>"rails4444", "tags"=>"ruby, rails, backbone, javascript", "date"=>"2014-02-22", "organizer"=>"backbonemeetupgroup", "description"=>"conference cool speakers", "talks"=>{"title"=>"fdsf", "video_url"=>"fdsf"},
where setting talks should created in database.
my html looks that:
<input type="text" name="talks[title]" placeholder="talk title" /> <input type="text" name="talks[video_url]" placeholder="url" />
and when updating conference model talks gives me error:
activerecord::associationtypemismatch (talk(#70154003251480) expected, got array(#70154000241560)): app/controllers/conferences_controller.rb:25:in `update'
my controller looks that:
def update @single = conference.find params[:id] if @single.update_attributes conference_params render "conferences/show" else respond_with @single end end def conference_params params.permit(:name, :tags, :date, :organizer, :description, :place, :talks => [:conference_id, :id, :title, :video_url]) end
why activerecord::associationtypemismatch error , how can fix it?
as conference
has_many
talks
, need array of talks nested attributes, not single one.
your input fields need names like:
<input type="text" name="conference[talks_attributes][0][title]" />
to work accepts_nested_attributes
. normal way create input use
<%= fields_for :talks |ff| %> <%= ff.text_field :title %> <% end %>
see rails guide on form helpers.
and need use talks_attributes
in permit
medbo suggests.
Comments
Post a Comment