2013-03-02 68 views
0

我想知道什么是在导轨中为不同页面设置导航链接的好方法?现在我已经休息了。下面是代码在导轨中设置导航栏/链接的好方法

_nav.html.erb 

<ul class="nav nav-pills pull-right"> 
    <%if current_page?(:action => 'new') %> 
    <li><%= link_to "Home", root_path%></li> 
    <%elsif current_page?(:controller => 'home', :action => 'index') %> 
    <li><%= link_to "Contact", home_contact_path%></li> 
    <%end%> 
</ul> 

所以,当我做一个<%= render :partial => 'layouts/nav' %>/views/home/index.html.erb它给了我一个错误

No route matches {:action=>"new", :controller=>"home"} 
+0

请提供'耙的输出routes' – gabrielhilal 2013-03-02 09:49:49

+0

这里的路线-http://pastebin.com/qpdAYDKw – psharma 2013-03-02 12:22:22

回答

1

你有没有新的动作在你的家控制器......看你的路线,只可用的行动是联系...

尝试使用助手(current_page?(helper))而不是action + controller。检查rake routes以查看帮助者。

_nav.html.erb 

<ul class="nav nav-pills pull-right"> 
    <%if current_page?(new_user_registration) %> #check the path helper by `rake routes` 
    <li><%= link_to "Home", root_path%></li> 
    <%elsif current_page?(root_path) %> 
    <li><%= link_to "Contact", home_contact_path%></li> 
    <%end%> 
</ul> 
+0

你好,请问我有一个辅助方法?这里是我的路线 - http://pastebin.com/qpdAYDKw – psharma 2013-03-02 12:22:45

+0

我编辑了答案...因此,如果current_page是new_user_registration,则链接将用于root_path ...如果current_page是根,则链接将为家居联系... – gabrielhilal 2013-03-02 16:00:58

+0

啊非常感谢。现在有意义:)。但current_page?方法是假设使用controller + action,如api所示。它没有任何特定的原因?还是我把它们定义错了? – psharma 2013-03-02 17:20:25