Answer the question
In order to leave comments, you need to log in
How does params work in Rails 4?
What to do if there can be many fields passed to the controller?
Rails 3.2:
def create
@item = Item.create(params[:item])
end
def create
@item = Item.create(items_params)
end
#Как работает данная конструкция?
private
def items_params
params.require(:item).permit(:name, :price, :description, :real, :weight)
end
end
Answer the question
In order to leave comments, you need to log in
What does a lot mean?
This design is designed to transfer control over the parameters available for mass editing from the model to the controller. Because that's where he belongs.
It works as follows. params is not really a regular hash, but an object of a certain class. When the model receives such an object as parameters, it asks it for a list of attributes that can be edited.
For example:
params = { user: { email: '[email protected]', password: '123' } }
params.require(:user).permit(:email)
. params.permit(:some_hash).require(some_array: [])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question