views:

422

answers:

3

In Rail 2.3.2 can I have merb style action definitions:

Eg: instead of

def show
  @user = User.find(params[:id])
end

Can I have:

def show(id)
  @user = User.find(id) 
end

What kind of crazy monkey patching do I need to do to get this working, note I only need this working for MRI so ParseTree is an option.

Note: there is a Rails 3 port of this functionality now.

A: 

I am running Rails 2.3.3 and User.find(id) works just fine.

askegg
notice the sig on def show(id), in rails actions have no params, they all get accessed via params see: http://journal.uggedal.com/looking-at-merb-again look how actions are defined
Sam Saffron
Opps. So you want to convert the params hash into local variables available to the controller? Or pass the params hash to the method according to the variables defined. These seems like a reasonably easy thing to achieve.
askegg
A: 

EDIT: I was wrong: Method#parameters has been added to Ruby 1.9.2.


Original:

No, this is not possible in Rails.

There is almost zero chance of this making it into Rails 3.0. merb-action-args used ParseTree which doesn't and will not work on Ruby 1.9, therefore making it unlikely that it will be included in Rails.

Olly
But its built in to ruby 1.9.2 and in 1.9.1 there is a work around ...
Sam Saffron
Actually it was just ported to Rails 3.0 ... http://github.com/adelcambre/rails-action-args/tree/master
Sam Saffron
A: 

You could change all params in to instance var's (eg. @id) in a before_filter...

Kris