2013-07-23 26 views
8

我遵循Michael Hartl的Rails指南(第9章)。当我尝试访问对应用户的个人资料页面时,浏览器会显示以下错误消息:用户控制器中的before_action呈现错误:用户#中的NoMethodError#show

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 

这里是我的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 

的页面加载罚款,而不行:

before_action :signed_in_user, only: [:edit, :update] 

但是因为它包含的东西出错了,我找不出原因。

而且,这里是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 

任何帮助表示赞赏!

+0

路线文件请问,当您访问此页面时,网址是什么。 – rmagnum2002

+0

这意味着你得到零用户的用户配置文件。 – Debadatt

+0

我刚刚在原帖中添加了路线文件。网址是http:// localhost:3000/users/1 –

回答

17

代替Users_controller试动作之前过滤 之前,因此,您的代码如下所示:

before_filter :signed_in_user, only: [:edit, :update] 

我有相同的错误!

1

继@Hurman古尔的回答,我希望给你,为什么你看到这个问题的一些细节......


你的控制器执行操作在一个不存在的变量

你有这个错误是因为你的一个动作被试图执行其上未设置变量的作用,或者没有内容(是)。错误基本上意味着你要调用一个变量,它不存在

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 

的“名称”的一部分这基本上意味着,在你看来,你试图加载@user .name,当@user可能不存在时。我建议解决这个问题的方法是最初适用一些逻辑到您的视图:

#app/views/users/show.html.erb 
<% unless defined?(@user) %> 
    Sorry, we could not find your user! 
<% else %> 

your code 

<% end %> 
1

我不得不使用4.1.6同样的问题。 问题是,你已经定义了before_action为private,我不知道为什么,但这种类型的可见性与控制器的Rails中的before_action负载混淆。所以只需在该区域外面带上signed_in_user,它应该再次开始工作。

希望这会有所帮助!