Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

named routes from string with object

I am looking to create a named route from a string and pass an object into it

I am rendering a partial with a local "resource"

ie. render :partial => "listing", :locals => {:resource => @resource}

inside _listing.html.haml i am using the self.send method

self.send("#{resource.class.to_s.downcase}_path()")

this works just fine and creates the resources named route ie. article_path()

but this route needs an object passed in the route ie. article_path(article)

i am trying this

self.send("#{resource.class.to_s.downcase}_path("+resource+")")

and get the error can't convert Article into String

any suggestions?

like image 941
ben.m Avatar asked Jan 27 '12 11:01

ben.m


Video Answer


2 Answers

You should pass the arguments like this :

self.send("#{resource.class.to_s.downcase}_path", resource)
like image 50
Jef Avatar answered Oct 15 '22 08:10

Jef


You can simplify Jef's answer (you don't need to use a resource) like so:

send("#{controller_name}_path")
like image 23
Mike Bethany Avatar answered Oct 15 '22 06:10

Mike Bethany