简体   繁体   English

Michael Hartl教程的第10章中的“未定义的局部变量或方法'user'”

[英]“undefined local variable or method `user'” in Chapter 10 of Michael Hartl's tutorial

I'm getting a few errors all of a similar nature and I'm not sure how to fix them. 我遇到了一些性质相似的错误,而且不确定如何解决。 One issue I've been running into is the differences between the new version of the tutorial and the old (I know there are some differences). 我遇到的一个问题是本教程的新版本与旧版本之间的差异(我知道有一些差异)。

My specs are: 我的规格是:

Ruby version: 1.9.2p320 Ruby版本:1.9.2p320

Rails version: 3.2.13 Rails版本:3.2.13

Rspec: 2.11.1 R规格:2.11.1

Computer: Macbook Pro OS X Mountain Lion 电脑:Macbook Pro OS X Mountain Lion

Errors 失误

5) Micropost pages micropost creation with invalid information should not create a micropost
     Failure/Error: expect { click_button "Post" }.not_to change(Micropost, :count)
     NameError:
       undefined local variable or method `user' for #<MicropostsController:0x007fc7a7c64078>
     # ./app/controllers/microposts_controller.rb:5:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/micropost_pages_spec.rb:16:in `block (5 levels) in <top (required)>'
     # ./spec/requests/micropost_pages_spec.rb:16:in `block (4 levels) in <top (required)>'

  6) Micropost pages micropost creation with invalid information error messages 
     Failure/Error: before { click_button "Post" }
     NameError:
       undefined local variable or method `user' for #<MicropostsController:0x007fc7a7897e90>
     # ./app/controllers/microposts_controller.rb:5:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/micropost_pages_spec.rb:20:in `block (5 levels) in <top (required)>'

  7) Micropost pages micropost creation with valid information should create a micropost
     Failure/Error: expect { click_button "Post" }.to change(Micropost, :count).by(1)
     NameError:
       undefined local variable or method `user' for #<MicropostsController:0x007fc7a7bdfb98>
     # ./app/controllers/microposts_controller.rb:5:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/micropost_pages_spec.rb:29:in `block (5 levels) in <top (required)>'
     # ./spec/requests/micropost_pages_spec.rb:29:in `block (4 levels) in <top (required)>'

Micropost_controller.rb Micropost_controller.rb

class MicropostsController < ApplicationController
  before_filter :signed_in_user

  def create
    @micropost = current user.microposts.build(params[:micropost])
    if @micropost.save
        flash[:success] = "Micropost created!"
        redirect_to root_path
    else
        render 'static_pages/home'
    end
  end

  def destroy
  end
end

micropost_pages_spec.rb micropost_pages_spec.rb

require 'spec_helper'

describe "Micropost pages" do

  subject { page }

  let(:user) { FactoryGirl.create(:user) }
  before { sign_in user }

  describe "micropost creation" do
    before { visit root_path }

    describe "with invalid information" do

      it "should not create a micropost" do
        expect { click_button "Post" }.not_to change(Micropost, :count)
      end

      describe "error messages" do
        before { click_button "Post" }
        it { should have_content('error') }
      end
    end

    describe "with valid information" do

      before { fill_in 'micropost_content', with: "Lorem ipsum" }
      it "should create a micropost" do
        expect { click_button "Post" }.to change(Micropost, :count).by(1)
      end
    end
  end
end

You wrote "current user" in your controller source code. 您在控制器源代码中编写了“当前用户”。 I bet you meant to write current_user . 我敢打赌你打算写current_user Identifiers in computer programming almost never have spaces in them. 计算机编程中的标识符几乎从来没有空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Michael Hartl教程的第10章中的“未定义的方法&#39;valid?&#39;” - “undefined method `valid?'” in chapter 10 of Michael Hartl's tutorial Michael Hartl的Rails教程第9章(第9.12节)测试失败“未定义的局部变量或方法” - Michael Hartl’s Rails Tutorial chapter 9 (section 9.12) tests failed “undefined local variable or method” rspec错误未定义局部变量或方法&#39;用户&#39;michael hartl教程 - rspec error undefined local variable or method 'user' michael hartl tutorial 迈克尔·哈特尔(Michael Hartl)的《 Rails教程》第10章销毁方法 - Michael Hartl's Rails Tutorial Chapter 10 destroy method Michael Hartl的Rails教程第12章-未定义的方法“用户” - Michael Hartl's Rails tutorial chapter 12 — undefined method `users' Michael Hartl教程的第11章中的“未定义的方法&#39;find_by&#39;” - “undefined method `find_by'” in Chapter 11 of Michael Hartl's tutorial Michael Hartl的教程第8章 - Michael Hartl's Tutorial Chapter 8 在Michael Hartl的Rail教程的第10章中,CSS没有更新 - CSS not updating in Chapter 10 of Michael Hartl's Rail's tutorial 未定义的局部变量或方法`help_path&#39;Hartl教程第8章 - undefined local variable or method `help_path' Hartl tutorial chapter 8 Michael Hartl的Rails教程第8章。未创建用户会话 - Michael Hartl's Rails tutorial chapter 8. User Session not created
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM