gpt4 book ai didi

ruby-on-rails - Michael Hartl 教程第 10 章中的 "undefined local variable or method ` 用户 '"

转载 作者:行者123 更新时间:2023-11-28 21:31:50 26 4
gpt4 key购买 nike

我遇到了一些性质相似的错误,但我不确定如何修复它们。我遇到的一个问题是教程的新版本与旧版本之间的差异(我知道存在一些差异)。

我的规范是:

ruby 版本:1.9.2p320

Rails 版本:3.2.13

Rspec: 2.11.1

计算机:Macbook Pro OS X Mountain Lion

错误

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)>'

微博_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

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

最佳答案

您在 Controller 源代码中写了“当前用户”。我打赌你打算写 current_user。计算机编程中的标识符几乎从不包含空格。

关于ruby-on-rails - Michael Hartl 教程第 10 章中的 "undefined local variable or method ` 用户 '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18175823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com