Reputation: 4612
I remember once seeing the ability to define at the top of a controller the parameters that an action requires to work. For the life of me I can't seem to find any mention of that capability anymore and it would be useful for the following:
def sort
params[:links].each_with_index do |id, index|
@link_set.links.in_context(context).update_all(['position=?', index+1], ['id=?', id])
end
render :nothing => true
end
I'm expecting params[:links] so if some smarty decides to visit that action without giving me params then it fails with nil.each_with_index.
It'd be nice to gracefully give a 404 or something if the parameters are missing without having to code this into every method of this type.
Does this exist? :)
Upvotes: 0
Views: 1078
Reputation: 4612
Actually, stumbled across this article: http://rails.nuvvo.com/lesson/6376-action-controller-verification
The method is called verify and you can specify things like parameters to expect. :)
Upvotes: 3
Reputation: 3375
Try this code:
before_filter :check_params
private
def check_params
needed_params=[:links]
needed_params.each do |x|
render :text=>"Missing param #{x}", :status=>400
return
end
end
Put this code at the end of your class. It will work for every action. Also, you want to return an error code 400 (Bad Request), not an error 404 (File Not Found).
Upvotes: 0
Reputation: 107728
I dont recall functionality existing that allows you to do that. You could use this instead at the top of your action:
params[:links] ||= []
Upvotes: -2