I'm trying to add user_id to a nested attribute that is created by the parent controller, but it doesn't seem to have the desired effect?
Those. I have a model called Place.rb which accepts_nested_attributes_for :reviewsandhas_many :reviews, :as => :reviewable, :dependent => :destroy
The nested attribute works fine, and I create it inside the Places controller so ...
new action
@review = @place.reviews.build(:user_id => current_user.id)
create action
params[:place].merge(:user_id => current_user.id)
params[:place][:reviews_attributes].merge!(:user_id => current_user.id)* bad
@place = Place.new(params[:place])
this is the original, for the place model, to get user_id, now I need user_id for the model of nested reviews. It may seem strange that there are user_ids in places and reviews, but people can add new reviews for the same place ...
maybe like this, but not working:
@place = Place.new(params[:place].merge(:user_id => current_user.id, :reviews_attributes => { :user_id => current_user.id } ))
get error: undefined methodwith_indifferent_access' for 3: Fixnum`
or
@place = Place.new(params[:place].merge(:user_id => current_user.id, :reviews_attributes => { "0" => { :user_id => current_user.id }}))
user_id, NULL; - (
, , user_id , - , user_id ...
, :
<%= e.label :content, "Review" %><br />
<%= e.text_area :content, :rows => 20, :class => 'jquery_ckeditor' %><br />
<%= e.hidden_field :user_id, :value => current_user.id %>
? ? ?
:
Parameters: {"commit"=>"Submit", "action"=>"create", "city_id"=>"prague",
"controller"=>"places", "place"=>{"address"=>"fsdfsdf", "name"=>"sdfsdfsd",
"reviews_attributes"=>{"0"=>{"content"=>"<p>\r\n\tsdfsdfsdfsdfsdfsdfsdf sdfsdfsdf</p>\r
\n"}}, "website"=>"", "city_id"=>"1036", "place_type"=>"1"}}