Cannot Assign Protected Attributes in Rails 4

I can't figure out what's wrong with my code (Rails 4):

parameters from the message:

{:name => "name"} 

new action:

 m=Menu.new(params.permit(:name)) 

The last line of this code generates "Cannot assign protected attributes to menu: name"

+6
source share
1 answer

The standard way to use strong_parameters in Rails 4 is to create a private method in the controller that defines the allowed parameters. For instance:

 def new @m = Menu.new(menu_params) end private def menu_params params.require(:menu).permit(:name, :etc, :etc) end 

You can then remove the attr_accessible string from the model.

See:

http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html http://railscasts.com/episodes/371-strong-parameters

+13
source

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


All Articles