views:

76

answers:

3

Hello,

Please look at the two examples below:

irb(#<ActionView::Base:0x2863d58>):030:0> paintings_path
=> "/some-nice-alias-path"

irb(#<ActionView::Base:0x2863d58>):029:0> self.controller_name.to_s + "_path"
=> "paintings_path"

How do I return the path instead of the string? I need to call the controller path from my application helper.

Best regards. Asbjørn Morell

+1  A: 

Just a guess, but something like:

method = self.controller_name.to_s + '_path'
self.send(method.to_sym)

will probably do the trick. The send method takes a symbol and executes the method matching that symbol on the target object.

mipadi
+3  A: 
 send(self.controller_name.to_s + "_path")

if you don't use send on self, it goes to Kernel, which is what you want. self might work though, depending on exact context.

Ben Hughes
Perfect. Worked like a charm. :)
atmorell
A: 

If you have an ActiveRecord instance to work with, perhaps polymorhpic_path will suit your needs. Using routing-aware path generation methods is preferable to brittle string-modification operations.

Duncan Beevers