不要在个人资料页面上显示导航栏 - (正确使用current_page?)

时间:2016-03-08 11:22:20

标签: ruby-on-rails ruby devise

我目前正在处理个人资料页面应用,我想实现这一点,如果有人在他的个人资料上访问用户(例如domain.com/username),则不会显示导航栏。如果用户已登录,则可以看到它。

我使用的是Rails 4.2.0,Ruby 2.2.3和Devise。

在我的application.html.erb中,我已经/尝试了

<% if user_signed_in? %>
   <%= render 'layouts/navbar' %>
<% else %>
   <%= render 'layouts/navbar' unless current_page?(controller: 'profiles', action: 'show')  %>
<% end %>

结果

No route matches {:action=>"show", :controller=>"devise/profiles"}

这是错误的,因为它调用设计控制器而不是配置文件控制器(用我的路线猜测它的东西)

我也试过

<%= render 'layouts/navbar' unless current_page?(profiles_show_url) %>

不会引发任何错误,但在未登录时仍会在用户个人资料上显示导航栏。

所以我只需要找到一种方法来调用路径,控制器动作,request.path或其他什么来使这项工作,我想它与我的路线有关。我也可能在这里扭曲了我的逻辑,可能是一种更优雅的方式来写这个。

非常感谢你的帮助!

更新

我现在尝试过:

<% unless user_signed_in? or current_page?(profiles_show_path)%>
   <%= render 'layouts/navbar' %>
<% end %>

当我使用profile_path时,它会在我转到domain.com/username时工作,但是当我去其他地方时它会抛出一个错误,因为它想要在转到索引时没有传递的:user_name,例如< / p>

No route matches {:action=>"show", :controller=>"profiles"} missing required keys: [:user_name]

rake routes:

                    Prefix Verb   URI Pattern                 Controller#Action
             profiles_show GET    /profiles/show(.:format)    profiles#show
          new_user_session GET    /login(.:format)            devise/sessions#new
              user_session POST   /login(.:format)            devise/sessions#create
      destroy_user_session DELETE /logout(.:format)           devise/sessions#destroy
             user_password POST   /password(.:format)         devise/passwords#create
         new_user_password GET    /password/new(.:format)     devise/passwords#new
        edit_user_password GET    /password/edit(.:format)    devise/passwords#edit
                           PATCH  /password(.:format)         devise/passwords#update
                           PUT    /password(.:format)         devise/passwords#update
  cancel_user_registration GET    /cancel(.:format)           registrations#cancel
         user_registration POST   /                           registrations#create
     new_user_registration GET    /sign_up(.:format)          registrations#new
    edit_user_registration GET    /edit(.:format)             registrations#edit
                           PATCH  /                           registrations#update
                           PUT    /                           registrations#update
                           DELETE /                           registrations#destroy
         user_confirmation POST   /confirmation(.:format)     devise/confirmations#create
     new_user_confirmation GET    /confirmation/new(.:format) devise/confirmations#new
                           GET    /confirmation(.:format)     devise/confirmations#show
                      root GET    /                           pages#index
                   profile GET    /:user_name(.:format)       profiles#show
              edit_profile GET    /:user_name/edit(.:format)  profiles#edit
            update_profile PATCH  /:user_name/edit(.:format)  profiles#update
                     posts GET    /posts(.:format)            posts#index
                           POST   /posts(.:format)            posts#create
                  new_post GET    /posts/new(.:format)        posts#new
                 edit_post GET    /posts/:id/edit(.:format)   posts#edit
                      post GET    /posts/:id(.:format)        posts#show
                           PATCH  /posts/:id(.:format)        posts#update
                           PUT    /posts/:id(.:format)        posts#update
                           DELETE /posts/:id(.:format)        posts#destroy

2 个答案:

答案 0 :(得分:1)

我注意到同一个动作profiles#show在路径文件中路由了两次。

请你试试这个,

在路线文件中注释此行get 'profiles/show'。并使用以下代码,

 <% if user_signed_in? && !current_page?(profile_path(current_user.user_name))%>
    <%= render 'layouts/navbar' %>
 <% elsif !current_page?(profile_path(params[:user_name])) %>
    <%= render 'layouts/navbar' %>
 <% end %>

上面的代码需要user_name表中的users列或相应的设计相关模型。

  

并确保profile_path正确使用。{   user_name在所有其他地方通过。

注意: - 如果它有效,你甚至可以将这个逻辑移动到辅助方法(yn)。

答案 1 :(得分:0)

我使用

解决了这个问题
<% if user_signed_in? or controller.controller_name != "profiles" %>
    <%= render 'layouts/navbar' %>  
<% end %>

这就是我需要的。

相关问题