Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing error in rails using current_page?

I'm trying to add some conditional open graph tags to my app, using current_page, but I'm getting a routing error:

This is what I get on rake routes

product GET    /products/:id(.:format)    {:action=>"show", :controller=>"products"}

And this is in my partial:

<% if current_page?(:controller => 'products', :action => 'show') %>
...
<% end %>

But I get:

Routing Error

No route matches {:controller=>"products", :action=>"show"}

I've tried using product and products but neither seems to work. Any thoughts?

like image 369
Slick23 Avatar asked Aug 30 '11 13:08

Slick23


1 Answers

You should pass the id as well:

<% if current_page?(:controller => 'products', :action => 'show', :id => params[:id]) %>

UPDATE:

But, however, this implementation assumes that the pages that render this partial have id as a param. If they do not, it's better to use a local variable for that.

render :partial => 'blah_blah', :locals => {:id_for_route_verification => params[:id] || 0}

and in the partial use the new local variable

<% if current_page?(:controller => 'products', :action => 'show', :id => id_for_route_verification) %>

I know this is kinda hacky. But, in the first place, you should consider using content_for to render content inside partial conditionally rather than going by current_page? approach

like image 164
dexter Avatar answered Sep 23 '22 22:09

dexter