簡體   English   中英

Rails鏈接錯誤消息和當前用戶

[英]Rails link with error message and current user

我很難提出一個如何實現名為“添加指南”的鏈接的想法,該鏈接需要用戶登錄。要定向到新指南路徑,我需要添加“ new_user_guide_path(@user)或“ new_user_guide_path(current_user)” ,然后加上“ current_user”或@user會導致錯誤,因為它等於nil-所以我不知道如何在向導控制器中實現新方法。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之后:

nil:NilClass的未定義方法'id'

因此,當沒有登錄用戶時,基本上當前用戶為nil-沒有太大的驚喜。 但是,由於有一些before_filters或before_action,是否有任何方法可以解決此錯誤,並提供指向需要用戶登錄而無需實際登錄的guuide的鏈接?

你可以這樣嘗試

`<% 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.idcurrent_user

這對我有用,希望對你也一樣:-)

在其他情況下添加“添加教程”鏈接:-

<% 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!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM