Reputation: 3696
I'm having trouble generating a url form an object.
I have following code in my routes file:
match '(*path)/:name' => 'pages#show'
Which directs to my Page
controller containing:
def show
if params[:path] and params[:name]
@page = Page.where(:path => params[:path], :title => params[:name]).first
elsif params[:name]
@page = Page.where(:path => "", :title => params[:name]).first
end
end
So in the case of localhost:3000/food/
will find the page where :path => "", :title => 'food'
And localhost:3000/food/pizza
will find the page where :path => "food/", :title => 'pizza'
The problem is trying get a url generated from the a Page
object.
So link_to(@page.title, @page)
returns:
undefined method 'page_path'
How do I get the route to work in reverse to generate a url form an object?
Upvotes: 0
Views: 3698
Reputation: 1293
You need to fall back to the 'old-style' link_to:
link_to @page.title, :controller => "pages", :action => "show", :path => @page.path, :name => @page.title %>
see: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to under examples
Also, check out http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default to see if you realy want to go down that road with your routes
Upvotes: 2