注册和登录时,nil的未定义方法`username':NilClass

时间:2013-11-29 21:17:26

标签: ruby-on-rails ruby authentication devise ruby-on-rails-4

每当我尝试登录并注册时,我都会遇到此错误:

nil的未定义方法`username':NilClass

这两行:

<strong><%= @user.username %></strong> <br>
<strong><%= @user.name %></strong>

以下是服务器输出的完整错误:

User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."username" = 'sign_in' LIMIT 1
  Rendered users/show.html.erb within layouts/application (2.1ms)
Completed 500 Internal Server Error in 7ms

ActionView::Template::Error (undefined method `username' for nil:NilClass):
    1: <strong><%= @user.username %></strong> <br>
    2: <strong><%= @user.name %></strong>
    3: <%= debug @user %>
    4: 
  app/views/users/show.html.erb:1:in `_app_views_users_show_html_erb__1133891108745893964_2164088020'

以下是我的路线:

Stynyl::Application.routes.draw do
  resources :things
  resources :users, only: [:show]

  devise_for :users
  get '/about', to: 'pages#about'
  root 'things#index'
end

这是我的用户显示视图

<strong><%= @user.username %></strong> <br>
<strong><%= @user.name %></strong>



<div id="things" class="transitions-enabled">
  <% @user.things.each do |thing| %>
    <div class='panel panel default'>
    <div class="box">
      <%= link_to image_tag(thing.image.url(:medium)), thing %>
      <div class='panel-body'>
      <strong><p><%= thing.title %></p></strong>
      <p><%= thing.description %></p>
      By <%= link_to thing.user.username, thing.user %>

      <% if thing.user == current_user %>
        <%= link_to edit_thing_path(thing) do %>
        <span class='glyphicon glyphicon-edit'></span> Edit
      <% end %>
      <%= link_to thing_path(thing), method: :delete, data: { confirm: 'Are you sure?' } do %>
        <span class='glyphicon glyphicon-trash'></span> Delete
      <% end %>
      </div>
      <% end %>
      </div>
    </div>
  <% end %>
</div>

这是我的UsersController文件:

class UsersController < ApplicationController
  def show
    @user = User.find_by_username(params[:id])
  end
end

rake路线的输出:

            Prefix Verb   URI Pattern                    Controller#Action
                  things GET    /things(.:format)              things#index
                         POST   /things(.:format)              things#create
               new_thing GET    /things/new(.:format)          things#new
              edit_thing GET    /things/:id/edit(.:format)     things#edit
                   thing GET    /things/:id(.:format)          things#show
                         PATCH  /things/:id(.:format)          things#update
                         PUT    /things/:id(.:format)          things#update
                         DELETE /things/:id(.:format)          things#destroy
                   users POST   /users(.:format)               users#create
                new_user GET    /users/new(.:format)           users#new
                    user GET    /users/:id(.:format)           users#show
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                   about GET    /about(.:format)               pages#about
                    root GET    /                              things#index

编辑

  1. 我还注意到,当我从User show视图中删除所有代码时,我可以访问注册和登录页面,但它们空白。我们能否安全地保证问题出在视图中?我很惊讶于节目视图中的某些内容会影响登录和注册的能力!
  2. 我还观察到,当我向用户show view添加内容时,它会出现在注册和登录页面上。到底是怎么回事?

1 个答案:

答案 0 :(得分:2)

原因是您的路线发生冲突。将请求与路由匹配时,Rails将按顺序遍历您的路由。但是您的订单显示:

user GET                /users/:id(.:format)           users#show
new_user_session GET    /users/sign_in(.:format)       devise/sessions#new

因此Rails将sign_in作为:id参数传递给您的用户show方法,而不是被设计new_user_session_path捕获。更改订单将解决问题。

TL:DR; devise_for :users应在 resources :users之前声明