未定义的方法'用户名'的零:NilClass签约并在签约时 [英] undefined method `username' for nil:NilClass when signing up and signing in
问题描述
每当我尝试登录,注册,我打这个错误:
未定义的方法'用户名'的零:NilClass
这两行:
<强><%= @ user.username%GT;< / STRONG> < BR>
<强><%= @ user.name%GT;< / STRONG>
下面是从服务器的输出完整的错误:
用户负载(0.3ms)选择用户。* FROM用户WHERE用户,用户名='sign_in'限制1
渲染用户/布局内show.html.erb /应用程序(2.1ms)
在7毫秒完成500内部服务器错误::的ActionView ::模板错误(未定义的方法'用户名'的零:NilClass):
1:<强><%= @ user.username%GT;< / STRONG> < BR>
2:<强><%= @ user.name%GT;< / STRONG>
3:其中;%=调试@user%GT;
4:
应用程序/视图/用户/ show.html.erb:1:`_app_views_users_show_html_erb__1133891108745893964_2164088020
下面是我的路线:
Stynyl :: Application.routes.draw做
资源:东西
资源:用户,只有:[:秀] devise_for:用户
得到'/约,到:网页#约
根'的东西#索引
结束
下面是我的用户显示视图
<强><%= @ user.username%GT;< / STRONG> < BR>
<强><%= @ user.name%GT;< / STRONG>< DIV ID =东西级=过渡启用>
<%@ user.things.each做|事情| %GT;
< DIV CLASS ='面板面板默认'>
< DIV CLASS =盒子>
<%=的link_to IMAGE_TAG(thing.image.url(:中)),事情%GT;
< DIV CLASS ='屏体'>
<强>< P><%= thing.title%GT;< / P>< / STRONG>
&所述p为H.;&下;%= thing.description%GT;&下; / P>
以<%=的link_to thing.user.username,thing.user%GT; <%如果thing.user == CURRENT_USER%GT;
<%=的link_to edit_thing_path(事)做%GT;
<跨度类='glyphicon glyphicon编辑'>< / SPAN>编辑
<%结束%GT;
<%=的link_to thing_path(事),方法:删除数据:{确认:你确定吗? }办%GT;
<跨度类='glyphicon glyphicon-垃圾'>< / SPAN>删除
<%结束%GT;
< / DIV>
<%结束%GT;
< / DIV>
< / DIV>
<%结束%GT;
< / DIV>
下面是我的UsersController文件:
类UsersController< ApplicationController中
高清节目
@user = User.find_by_username(PARAMS [:ID])
结束
结束
耙路输出:
preFIX动词URI模式控制器#行动
事情变得/things(.:format)的东西#指数
POST /things(.:format)的东西#创建
new_thing GET /things/new(.:format)的东西#新
edit_thing GET /things/:id/edit(.:format)的东西#编辑
事情GET /things/:id(.:format)的东西#秀
PATCH /things/:id(.:format)的东西#更新
PUT /things/:id(.:format)的东西#更新
DELETE /things/:id(.:format)的东西毁灭#
网友发帖/users(.:format)的用户创建#
new_user GET /users/new(.:format)用户#新
用户GET /users/:id(.:format)的用户#秀
new_user_session GET /users/sign_in(.:format)设计/会话#新
USER_SESSION POST /users/sign_in(.:format)设计/会话创建#
destroy_user_session DELETE /users/sign_out(.:format)设计/会话#销毁
USER_PASSWORD POST /users/password(.:format)制订/密码#创建
new_user_password GET /users/password/new(.:format)设计/密码#新
edit_user_password GET /users/password/edit(.:format)设计/密码#编辑
PATCH /users/password(.:format)设计/密码#更新
PUT /users/password(.:format)设计/密码#更新
cancel_user_registration GET /users/cancel(.:format)设计/注册#取消
user_registration POST /users(.:format)设计/注册#创建
new_user_registration GET /users/sign_up(.:format)设计/注册#新
edit_user_registration GET /users/edit(.:format)设计/注册#编辑
PATCH /users(.:format)设计/注册#更新
PUT /users(.:format)设计/注册#更新
DELETE /users(.:format)设计/注册#销毁
有关GET /about(.:format)网页约#
根GET /东西#指数
编辑
- 我也注意到,当我删除一切从用户显示视图中的code,我可以得到的注册和登录页面,但他们的空白。我们可以有把握地保证,这个问题是在视图中?我的东西在Show View如何影响登录并注册!的能力,使困惑
- 我还观察到,当我将内容添加到用户显示视图,它出现在注册和登录页面。这到底是怎么回事?
究其原因是因为在你的路由冲突。当匹配到路由的请求时,Rails会通过你的路线顺序。但是,您的订单显示:
用户GET /users/:id(.:format)的用户#秀
new_user_session GET /users/sign_in(.:format)设计/会话#新
所以Rails正在通过 sign_in
为:ID
参数到您的用户显示的方法,而不是被抓由色器件的 new_user_session_path
。更改订单将解决这个问题。
TL:DR; devise_for:用户
应申报的前的资源:用户
Whenever I try to log in and sign up, I am hit with this error:
undefined method `username' for nil:NilClass
on these two lines:
<strong><%= @user.username %></strong> <br>
<strong><%= @user.name %></strong>
Here is the full error from the server's output:
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'
Here are my routes:
Stynyl::Application.routes.draw do
resources :things
resources :users, only: [:show]
devise_for :users
get '/about', to: 'pages#about'
root 'things#index'
end
Here is my user show view
<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>
Here is my UsersController file:
class UsersController < ApplicationController
def show
@user = User.find_by_username(params[:id])
end
end
Output of rake routes:
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
Edits
- I also noticed that when I delete all of the code from the User show view, I can get to the signup and login pages, but they are blank. Can we safely guarantee that the problem is in the view? I'm so perplexed at how something in the show view can affect the ability to sign in and sign up!
- I have also observed that when I add content to the users show view, it appears on the sign up and login pages. What on earth is going on?
The reason is because of a conflict in your routes. When matching a request to a route, Rails will go through your routes sequentially. But your order shows:
user GET /users/:id(.:format) users#show
new_user_session GET /users/sign_in(.:format) devise/sessions#new
And so Rails is passing sign_in
as the :id
parameter to your user show method, instead of being caught by devise's new_user_session_path
. Changing the order will fix the problem.
TL:DR; devise_for :users
should be declared before resources :users
.
这篇关于未定义的方法'用户名'的零:NilClass签约并在签约时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!