Search code examples
ruby-on-railsactiverecordrails-activerecordruby-on-rails-5rails-models

After renaming a model, why do my create and update functions fail?


I renamed one of my models and its associated table name, controller, view folder, and references of the old name throughout all files in the app. The app runs fine except I'm now unable to create or update Actions (new name) because of an error related to params. Here is the error received when creating a new Action:

undefined method `permit' for "create":String Did you mean? print

Here are the params shown with this error:

Parameters:

{"utf8"=>"✓", "authenticity_token"=>"[removed]", "commit"=>"Create This Campaign"}

I manually replaced the token with [removed] here.

I receive the same error when trying to update an Action:

undefined method `permit' for "update":String Did you mean? print

And here are the parameters shown with this update error:

Parameters:

{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"[removed]", "commit"=>"Submit", "id"=>"50"}

Before I renamed everything, these errors did not appear. Any idea why this is occurring? It looks like my app is passing a string (instead of a hash) to params.require(:action).permit, but I'm not sure why it would be doing that.


Solution

  • You shouldn't use action as a resource name in Rails. The action parameter in params is always set to the name of the action being invoked, meaning you cannot use params[:action] to post data back to your server.

    In a controller's update action, params[:action] will always be the string "update", hence the error you're getting about permit not being defined on the string "update".