Ruby-on-rails 用户控制器渲染错误中的before_action:用户中的NoMethodError#show
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17807475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
before_action in user controller rendering error: NoMethodError in Users#show
提问by i_trope
I am following Michael Hartl's Rails guide (on chapter 9). When I try to access the page corresponding the the user's profile, the browser displays the following error messages:
我正在关注 Michael Hartl 的 Rails 指南(第 9 章)。当我尝试访问与用户个人资料对应的页面时,浏览器显示以下错误消息:
NoMethodError in Users#show
Showing /home/jonathan/Desktop/railsTut/sample_app/app/views/users/show.html.erb where line #1 raised:
undefined method `name' for nil:NilClass
Extracted source (around line #1):
1: <% provide(:title, @user.name) %>
2: <div class="row">
3: <aside class="span4">
4: <section>
Rails.root: /home/jonathan/Desktop/railsTut/sample_app
Here is my users_controller.rb
这是我的 users_controller.rb
class UsersController < ApplicationController
before_action :signed_in_user, only: [:edit, :update]
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user]) # Not the final implementation!
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
# Before filters
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
end
The page loads fine without the line:
页面加载正常,没有该行:
before_action :signed_in_user, only: [:edit, :update]
But with it's inclusion things go wrong and I can't figure out why.
但是随着它的包含,事情出错了,我不知道为什么。
Also, here is the routes.rb
另外,这里是 routes.rb
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root to: 'static_pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
end
Any help is appreciated!
任何帮助表示赞赏!
回答by frbl
If indeed Hurman's answer solved your problem, the reason is because you are running an older version (i.e. <4.0.0) of rails. before_filterwas renamed in this version to before_action(https://github.com/rails/rails/commit/9d62e04838f01f5589fa50b0baa480d60c815e2c), more information in Rails 4: before_filter vs. before_action
如果 Hurman 的回答确实解决了您的问题,原因是您运行的是旧版本(即 <4.0.0)的 rails。before_filter在此版本中更名为before_action( https://github.com/rails/rails/commit/9d62e04838f01f5589fa50b0baa480d60c815e2c),Rails 4 中的更多信息:before_filter vs. before_action
回答by Hurman Gul
Instead of Before Actionin Users_controllertry before filter So your code looks like this:
而不是在Users_controller 中的Before Action 之前尝试过滤器所以你的代码看起来像这样:
before_filter :signed_in_user, only: [:edit, :update]
I had the exact same error!
我有完全相同的错误!
回答by WyrmxD
I had the same problem using 4.1.6.
The problem is that you have defined the before_action as private, I don't know why but this type of visibility messes with the controller's before_action load in Rails. So just bring signed_in_useroutside that area and it should start working again.
我在使用4.1.6 时遇到了同样的问题。问题是您已将 before_action 定义为private,我不知道为什么,但这种类型的可见性与 Rails 中控制器的 before_action 负载混淆。所以只要带出signed_in_user那个区域,它就会重新开始工作。
Hope this helps!
希望这可以帮助!
回答by Richard Peck
Further to @Hurman Gul's answer, I'll hopefully give you some details about why you're seeing this problem...
除了@Hurman Gul 的回答之外,我希望能向您提供一些有关您为什么会看到此问题的详细信息...
Your Controller Is Performing An Action On A Non-Existent Variable
您的控制器正在对不存在的变量执行操作
The error you have is because one of your actions is trying to perform an action on a variable which is either not set, or has no content (is nil). The error basically means that you're trying to call the ".name" part of a variable, where it doesn't exist
您遇到的错误是因为您的一项操作试图对未设置或没有内容(为nil)的变量执行操作。该错误基本上意味着您正在尝试调用不存在的变量的“.name”部分
undefined method `name' for nil:NilClass
Extracted source (around line #1):
1: <% provide(:title, @user.name) %>
2: <div class="row">
3: <aside class="span4">
4: <section>
Rails.root: /home/jonathan/Desktop/railsTut/sample_app
This basically means that in your view, you're trying to load @user.name, when @user probably doesn't exist. The way I'd recommend fixing this is to initially apply some logic to your view:
这基本上意味着在您看来,您正在尝试加载 @user.name,而 @user 可能不存在。我建议解决此问题的方法是最初将一些逻辑应用于您的视图:
#app/views/users/show.html.erb
<% unless defined?(@user) %>
Sorry, we could not find your user!
<% else %>
your code
<% end %>

