You can whitelist parameters for any action in exactly the same way:
Foo.new( params.require(:foo).permit(:bar) )
# or for a flat hash
Foo.new( params.permit(:foo, :bar) )
The params hash is just a hash like object which has a permitted
flag. Calling .permit
returns a new ActionController::Parameters
instance containing only the permitted keys and with the permitted flag set to true.
But this is the wrong answer to the wrong question.
This issue at hand is not whitelisting parameters in the index, edit and show parameters - rather the problem is that your application is using the parameters to create / update models from a GET request in the first place.
The routes for index, edit and show actions all correspond to GET requests which should be idempotent (they should not alter resources at all). This is especially important since they are saved in the browsers history which can lead to unexpected consequences if the user presses the back button.
Prefix Verb URI Pattern Controller#Action
things GET /things(.:format) things#index
POST /things(.:format) things#create
new_thing GET /things/new(.:format) things#new
edit_thing GET /things/:id/edit(.:format) things#edit
thing GET /things/:id(.:format) things#show
PATCH /things/:id(.:format) things#update
PUT /things/:id(.:format) things#update
DELETE /things/:id(.:format) things#destroy
While GET methods can take parameters they should generally not be doing any mass assignment to a model.
Only the create
and update
action which correspond to the non-idempotent POST
and PUT|PATCH
methods should be concerned with param whitelisting. You should ensure that your ajax call is being sent to the correct path and uses the correct HTTP method.