In case you forgot, Montreal on Rails #2 is tonight! Hope to see you there.
Hello, I’m Ruby on Rails!
Cool/funny/stupid stuff, Ruby on Rails, Software development, Technology 1 Comment »Here’s the geekiest thing I’ve seen this week (and I love it!) A Rails vs. Java ad, Mac/PC style!
smart_params: unified id params across your controllers
Ruby on Rails, Software development No Comments »I am in a situation where I need a single method to perform miscellaneous operations from different controllers. I am using RESTful routes and I ran into the problem where the last ID in the URL has to be accessed by params[:id] instead of params[:controller_id] like the other IDs of the same URL.
In a nutshell, I had:
URL: /families/1/people/5
However, for the sake of simplicity and to be able to use the method in question from other controllers (ex: /families/1/people/5/friends/7) without any modifications, I wanted to do person_id = params[:person_id] regardless of which controller was being accessed.
This was fairly easy to fix and I rolled out a little helper method I call smart_params:
I put it in /app/controllers/application.rb and added before_filter :smart_params to the class. Voila! Unified and consistent params for your IDs!
I don’t know if you’ll find this useful for anybody else, but it’s saving me a lot of time.
EDITS:
March 3, 2007: modified smart_params slightly to avoid creating a new nil param when params[:id] does not exist.
Recent Comments