Hi –
On Sat, 3 Feb 2007, kris wrote:
before_filter :load_client
before_filter :load_parent :client
Don’t forget the comma between arguments in method calls
Then have load_parent take :symbol and do @(symbol.to_s) =
symbl.to_s.findparams[:symbol_id]
Hopefully this makes sense.
I didn’t see Rick’s suggestion, so I don’t know whether this is
similar, but here’s one possibility. (If you don’t understand it,
there’s a book I can recommend that serves the specific purpose of
explaining Ruby in depth to Rails developers
First, in application.rb (the parent controller file), write your
load_param (or parent, but I think param makes more sense) method. In
fact, make it plural, so that you can handle more than one symbol at a
time:
def load_params(*symbols)
symbols.each do |sym|
c = Object.const_get(Inflector.classify(sym))
instance_variable_set(“@#{sym}”, c.find(params[“#{sym}_id”]))
end
end
That method uses the utility object Inflector to get the class-like
version of sym (:x becomes “X”, :some_thing becomes “SomeThing”,
etc.). That string can then be used by Object.const_get to get the
actual class object.
The next line does the actual setting of the instance variable.
Now, in the controller where you want this to happen, you can do:
before_filter {|c| c.load_params(:a,:b,:c) }
When you give a code block to before_filter, the controller object
itself gets yielded to the block; that’s c. So you can then call
load_param on c, passing in the params you want to set.
There are other ways to engineer this, but that’s one way that gives
you a pretty high-level interface in the controller itself.
David
–
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (Ruby for Rails)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)