For create in a controller, the data is passed into the controller in params[:controller_name]
(controller_name could be article or product for example) We can use params[:field1]
to access the field1
data(WRONG!). We know the data is passed in as controller_name -> {field1 = A, field2 = B ...}
. Is params[:controller_name][:field1]
the same as params[:field1]
?
Can anyone explain? Thanks.
I think controller_name is a reserved param that always holds the name of the controller so that could explain any weird behavior you are seeing. Try using :cname or something like that instead.
The answer is: the right way to retrieve a field from params is params[:controller_name][:field_name]
, the controller_name here could be for example, product or article.
params[:field_name]
only returns nothing in our test.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.