Assembly parameter issues for accepts_nested_attributes_for

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 %> #want to remove this line

? ? ?

:

    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"}}
+3
4

:

params[:place][:user_id] = current_user.id
params[:place][:reviews_attributes].each do |key, review|
  review[:user_id] = current_user.id
end if params[:place][:reviews_attributes]
+4

, params[:review] , merge!:

params[:review].merge!(:user_id => current_user.id)
@review = @place.reviews.build(params[:review])

Edit: , create.

# 2: new, , railsapi.com,

" , , nil!"

# 3: , , , ...

:

{"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"}}

, : params[:place][:reviews_attributes] , user_id, :

params[:place][:reviews_attributes].each_value {
   |v| v.merge!(:user_id => current_user.id) 
}

params[:place][:reviews_attributes] :

{"0"=>{
   "user_id"=>"1",
   "content"=>"<p>\r\n\tsdfsdfsdfsdfsdfsdfsdf sdfsdfsdf</p>\r\n"
}}
+1

, user_id. params . , current_user.id. , , ?

+1

, form_for . hidden_fields, : http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

: ( , !)

nachocab - 2 2008 . params hash

params form_for. :

URL://209//new form_for [@album, @song] do | f |     ...     f.submit "" end

hash :

params = { "commit" = > "" ,              "Authenticity_token" = > "...",              "Album_id" = > "209",              "" = > { "song_attributes" = > {...}}             }

, song_controller _ before_filter:

before_filter: find_album
protected def find_album     @album = Album.find(params [: album_id]) end

:

form_for @song do | f |

hash:

params = {"commit" => "Add", "Authenticity_token" => "...", "Song" => {"song_attributes" => {...}}}

0
source

Source: https://habr.com/ru/post/1742996/


All Articles