开发者

Routing error in rails using current_page?

开发者 https://www.devze.com 2023-03-31 14:27 出处:网络
I\'m trying to add some conditional open graph tags to my app, using current_page, but I\'m getting a routing error:

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=>开发者_如何学Python"show"}

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消