用户控制器渲染错误中的before_action:用户#show中的NoMethodError

时间:2013-07-23 10:22:51

标签: ruby-on-rails railstutorial.org

我正在关注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

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

感谢任何帮助!

4 个答案:

答案 0 :(得分:18)

如果Hurman的答案确实解决了您的问题,原因是因为您运行的是旧版本(即<4.0.0)的rails。 before_filter已在此版本中重命名为before_actionhttps://github.com/rails/rails/commit/9d62e04838f01f5589fa50b0baa480d60c815e2c),Rails 4: before_filter vs. before_action

中的更多信息

答案 1 :(得分:16)

而不是 Users_controller 中的行动前,请在过滤前尝试 所以你的代码看起来像这样:

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

我有完全相同的错误!

答案 2 :(得分:2)

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

希望这有帮助!

答案 3 :(得分:1)

继@Hurman Gul的回答,我希望能给你一些关于你为什么会看到这个问题的细节......


您的控制器正在对不存在的变量执行操作

您遇到的错误是因为您的某个操作正在尝试对未设置的变量执行操作,或者没有内容( 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

这基本上意味着在您的视图中,当@user可能不存在时,您正在尝试加载@ user.name。我建议修复此问题的方法是首先在视图中应用一些逻辑:

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

 your code

<% end %>