How can i add user_id into params[:page] i don't want to use hidden fields.
@page= Page.new(params[:page])
Is there a way to use like
@page= Page.new(:name=>params[:page][:name], :user_id => current_user.id)
Specifically, params refers to the parameters being passed to the controller via a GET or POST request. then the controller would pass in {:name => “avi”} to the show method, which would set the @person instance variable to the person in the database with the name “avi”.
Strong Parameters, aka Strong Params, are used in many Rails applications to increase the security of data sent through forms. Strong Params allow developers to specify in the controller which parameters are accepted and used.
Returns a parameter for the given key.
I use this day in and day out:
@page= Page.new(params[:page].merge(:user_id => 1, :foo => "bar"))
Instead of doing it that way, build the association (assumes you have has_many :pages
in the User
model):
@page = current_user.pages.build(params[:page])
This will automatically set user_id
for the Page
object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With