我很难想出如何实现一个名为"添加指南"的链接,这需要用户登录。要指向新的指南路径,我需要添加" new_user_guide_path(@user)或" new_user_guide_path(current_user)",并添加" current_user"或@user由于等于零而导致错误 - 所以我不知道如何在指南控制器中实现新方法。 PS:我使用devise gem来创建用户模型。 我的标题部分:
<header class="cf header2">
<h1 class="logo"><%= link_to "E-LEARN", root_path %></h1>
<nav>
<ul>
<% unless user_signed_in? %>
<li><%= link_to "Log In", new_user_session_path %></li>
<% else %>
<li><%= link_to "Dashboard", root_path %></li>
<li><%= link_to "Log Out", destroy_user_session_path, method: :delete %></li>
<li><%= link_to "Settings", edit_user_registration_path %></li>
<li><%= link_to "My Account", current_user %></li>
<% end %>
<% if user_signed_in? %>
<li><%= link_to "Add tutorial", new_user_guide_path(@user) %></li>
<% end %>
</ul>
</nav>
</header>
和路线:
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:show, :index, :new] do
resources :guides
end
authenticated :user do
root to: 'users#dashboard', as: "authenticated_root"
end
root 'welcome#index'
end
错误讯息:
Started GET "/users/sign_in" for 127.0.0.1 at 2015-11-23 23:25:13 +0100
Processing by Devise::SessionsController#new as HTML
Rendered shared/_header.html.erb (2.2ms)
Rendered devise/sessions/new.html.erb within layouts/application (3.1ms)
Completed 500 Internal Server Error in 7ms (ActiveRecord: 0.0ms)
ActionView::Template::Error (No route matches {:action=>"new", :controller=>"guides", :user_id=>nil} missing required keys: [:user_id]):
10: <li><%= link_to "Settings", edit_user_registration_path %></li>
11: <li><%= link_to "My Account", current_user %></li>
12: <% end %>
13: <li><%= link_to "Add tutorial", new_user_guide_path(current_user) %></li>
14: </ul>
15: </nav>
16: </header>
app/views/shared/_header.html.erb:13:in `_app_views_shared__header_html_erb__869860312033883396_70024564174000'
app/views/devise/sessions/new.html.erb:1:in `_app_views_devise_sessions_new_html_erb___1648951009615067685_70024756409300'
在我改为current_user.id之后:
未定义的方法`id&#39;为零:NilClass
因此,当没有登录用户时,基本上当前用户为零 - 没有太大的惊喜。但有没有办法解决这个错误,感谢一些before_filters或before_action并包含添加guuide的链接,需要登录用户数据,而无需用户实际登录?
答案 0 :(得分:0)
你可以试试这种方式
`<% if signed_in? %>
<%= link_to "Profile", profile_path %>
<% if can? :update, @user %>
<%= link_to "Admin", admin_root_path %>
<% end %>
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "Sign in", new_user_session_path %>
<%= link_to "Join Us", new_user_registration_path %>
<% end %>`
并改变你的
current_user.id
至current_user
这对我有用,希望它能为你做同样的事情: - )
答案 1 :(得分:0)
在其他情况下添加“添加教程”链接: -
<% if user_signed_in? %>
<li><%= link_to "Dashboard", root_path %></li>
<li><%= link_to "Log Out", destroy_user_session_path, method: :delete %></li>
<li><%= link_to "Settings", edit_user_registration_path %></li>
<li><%= link_to "My Account", current_user %></li>
<li><%= link_to "Add tutorial", new_user_guide_path(current_user) %></li>
<% else %>
<li><%= link_to "Log In", new_user_session_path %></li>
<% end %>
在指南控制器中添加before_filter: -
before_filter :authenticate_user!