2013-07-23 28 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,它應該再次開始工作。

希望這會有所幫助!