I've been using Rails for about 4 years and have often wondered why it doesn't implement the following concept; a Rails controller could define a method like:
def mymethod(a, b)
end
and if you invoked the method with mymethod&a=14&b=hi that Rails would automatically set the variable "a'" to "14" and "b" to "hi".
Right now, you have to write methods like:
def mymethods
a = params[:a]
b = params[:b]
...
end
Does anyone have any insight into why Rails doesn't support this?
Perhaps surprisingly ruby has no means to get parameter names.
Merb (presumably to achieve what you describe here) has 'action-args'. The implementation is quite exciting, one for each of jruby, mri and 1.9.
http://merbivore.com/documentation/1.0.6.1/doc/rdoc/merb-action-args-1.0/index.html
Posted by: Damian | Oct 27, 2010 at 02:00 AM
I don't believe that Ruby itself provides the necessary function to do this: if you get access to a method, you can determine its "arity" but not the names of arguments.
The closest you could come is something like this:
action :mymethods, [:a, :b] do
end
For bonus points, the action method could define the necessary route.
Posted by: intertwingly.net/blog | Oct 27, 2010 at 06:32 AM
According to this, there are a few ways to get method names via reflection:
http://stackoverflow.com/questions/622324/getting-argument-names-in-ruby-reflection
Perhaps this is something new in Ruby 1.9?
Posted by: Graham Glass | Oct 27, 2010 at 11:22 AM
This might show Rails's heritage from a fed-up PHP developer. Having to set the variables from the param[] array might help prevent against security attacks and trying to chase down order-of-insertion bugs.
Posted by: Robert W. Oliver II | Oct 27, 2010 at 10:41 PM