【问题标题】:RoR Tutorial Hartl Chapter 9.3RoR 教程 Hartl 第 9.3 章
【发布时间】:2012-06-09 00:59:51
【问题描述】:

我真的被教程卡住了,并且一直在慢慢地把它拉回原形。 (我是 2 周的新手)。当我跑步时

bundle exec rspec spec/requests/user_pages_spec.rb -e "edit page"

我明白了

sis-macbook-pro:sample_app Lagaspi$ bundle exec rspec spec/requests/user_pages_spec.rb -e "edit page"
Run options: include {:full_description=>/(?-mix:edit\ page)/}
FFF

Failures:

1) User pages signup with valid information edit page 
 Failure/Error: before { visit edit_user_path(user) }
 ActionView::Template::Error:
   undefined method `model_name' for NilClass:Class
 # ./app/views/users/edit.html.erb:6:in `_app_views_users_edit_html_erb___432836341465923353_70096274496380'
 # ./spec/requests/user_pages_spec.rb:96:in `block (5 levels) in <top (required)>'

2) User pages signup with valid information edit page 
 Failure/Error: before { visit edit_user_path(user) }
 ActionView::Template::Error:
   undefined method `model_name' for NilClass:Class
 # ./app/views/users/edit.html.erb:6:in `_app_views_users_edit_html_erb___432836341465923353_70096274496380'
 # ./spec/requests/user_pages_spec.rb:96:in `block (5 levels) in <top (required)>'

3) User pages signup with valid information edit page 
 Failure/Error: before { visit edit_user_path(user) }
 ActionView::Template::Error:
   undefined method `model_name' for NilClass:Class
 # ./app/views/users/edit.html.erb:6:in `_app_views_users_edit_html_erb___432836341465923353_70096274496380'
 # ./spec/requests/user_pages_spec.rb:96:in `block (5 levels) in <top (required)>'

Finished in 0.29628 seconds
3 examples, 3 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:99 # User pages signup with valid information edit page 
rspec ./spec/requests/user_pages_spec.rb:100 # User pages signup with valid information edit page 
rspec ./spec/requests/user_pages_spec.rb:101 # User pages signup with valid information edit page 

这是我的 /spec/requests/user_pages_spec.rb

require 'spec_helper'

describe "User pages" do

subject { page }

describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }

it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end

describe "signup page" do
before { visit signup_path }

it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }
end

describe "signup" do

before { visit signup_path }

describe "with invalid information" do
  it "should not create a user" do
    expect { click_button "Create my account" }.not_to change(User, :count)
  end

  describe "error messages" do
    before { click_button "Create my account" }

    it { should have_selector('title', text: 'Sign up') }
    it { should have_content('error') }
  end
end

  describe "with valid information" do
  before do
    fill_in "Name", with: "Example User"
    fill_in "Email", with: "user@example.com"
    fill_in "Password", with: "foobar"
    fill_in "Confirmation", with: "foobar"
  end

  it "should create a user" do
    expect do
      click_button "Create my account"
    end.to change(User, :count).by(1)
  end

  describe "after saving the user" do
    before { click_button "Create my account" }
    let(:user) { User.find_by_email('user@example.com') }

    it { should have_selector('title', text: user.name) }
    it { should have_selector('div.alert.alert-success', text: 'Welcome') }
    it { should have_link('Sign out') }
   end
 end
end

describe "signup page" do
before { visit signup_path }

it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }
end

    describe "signup" do

before { visit signup_path }

let(:submit) { "Create my account" }

describe "with invalid information" do
  it "should not create a user" do
    expect { click_button submit }.not_to change(User, :count)
  end
end

describe "with valid information" do
  before do
    fill_in "Name", with: "Example User"
    fill_in "Email", with: "user@example.com"
    fill_in "Password", with: "foobar"
    fill_in "Confirmation", with: "foobar"
  end

  it "should create a user" do
    expect { click_button submit }.to change(User, :count).by(1)
  end
  describe "edit" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit edit_user_path(user) }

    describe "page" do
      it { should have_selector('h1', text: "Update your profile") }
      it { should have_selector('title', text: "Edit user") }
      it { should have_link('change', href: 'http://gravatar.com/emails') }
    end

    describe "with invalid information" do
      before { click_button "Save changes" }

      it { should have_content('error') }
      end
    end
   end
 end
end

有什么想法吗?

users_controller.rb

class UsersController < ApplicationController

def show
@user = User.find(params[:id])
end

def new
@user = User.new
end

def create
@user = User.new(params[:user])
if @user.save
  sign_in @user
  flash[:success] = "Welcome to the Sample App!"
  redirect_to @user
else
  render 'new'
   end
 end
end

【问题讨论】:

  • 你能把你的edit.html.erb的第6行贴到views/users文件夹中吗?您在用户控制器的编辑操作中还有什么代码?
  • 6
  • 嗯,所以@user 肯定是零。您是否在编辑操作中进行了设置?
  • 对不起,新手 - 我不明白
  • 在 OP 中添加 users_controller.rb 代码

标签: ruby-on-rails ruby


【解决方案1】:

您的问题似乎是您没有编辑操作。添加这个

def edit
    @user = User.find(params[:id])
end

到您的用户控制器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多