我一直在尝试通过其他问题提出一些建议,但无法弄清楚这一点。 Hartl教程Ch。 9.1.1 http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users
和BTW我正在运行此测试:$ bundle exec rspec spec / requests / user_pages_spec.rb -e“edit page”
我也没有访问更新个人资料页面的用户界面。我没见过图9.2 URI localhost:3000 / users / 1 / edit 有这个错误:NilClass的未定义方法`model_name':Class(ActionController :: ExceptionCaught) 对于edit.html.erb文件的第6行:
4: <div class="row">
5: <div class="span6 offset3">
6: <%= form_for(@user) do |f| %>
模型名称的未定义方法是否来自form_for?
在RSpec方面,我有3次失败。 我先提到Rspec错误
Failures:
1) User pages signup 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__1823978725744522424_2169171460'
# ./spec/requests/user_pages_spec.rb:56:in `block (4 levels) in <top (required)>'
2) User pages signup 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__1823978725744522424_2169171460'
# ./spec/requests/user_pages_spec.rb:56:in `block (4 levels) in <top (required)>'
3) User pages signup 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__1823978725744522424_2169171460'
# ./spec/requests/user_pages_spec.rb:56:in `block (4 levels) in <top (required)>'
Finished in 0.435 seconds
3 examples, 3 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:59 # User pages signup edit page
rspec ./spec/requests/user_pages_spec.rb:60 # User pages signup edit page
rspec ./spec/requests/user_pages_spec.rb:61 # User pages signup edit page
这是我的Edit.html.erb页面:
<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
<% gravatar_for @user %>
<a href="http://gravatar.com/emails">change</a>
</div>
</div>
这是我的用户规范页面
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 submit }.not_to change(User, :count)
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 "after saving the user" do
before { click_button submit }
#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 "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
这是我的身份验证页面规范:
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_selector('h1', text: 'Sign in') }
it { should have_selector('title', text: full_title('Sign in')) }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_selector('title', text: 'Sign in') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
it { should have_selector('title', text: user.name) }
it { should have_link('Profile', href: user_path(user)) }
#it { should have_link('Settings', href: edit_user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path)}
describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
end
end
end
这是我的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'
这是我的用户控制器,似乎完好无损
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 Your App!"
redirect_to @user
else
render 'new'
end
def edit
@user = User.find(params[:id])
end
end
end
我将我的Capybara从1更新到2.0.0,并尝试了许多其他的东西。我对Rails很陌生,而且我把头发拉出来了!
答案 0 :(得分:1)
值得注意的是,编辑测试页应该从列表9.1传递,但错误消息测试#it {should have_content(&#39; error&#39;)}#不会通过直到你完成9.1.2中的练习
答案 1 :(得分:0)
你有不平衡的方法陈述:
class UsersController < ApplicationController
# correctly terminated
def show
end
# correctly terminated
def new
end
# this method is not terminated correctly as there should be another "end" statement
def create
if @user.save
else
end
# Put an extra "end" statement here by un-commenting the line below
# end
# the missing end statement above means this method is defined inside #create
def edit
end
# one of the following two "end" statements is superfluous delete it
end
end
答案 2 :(得分:0)
检查您的用户控制器,正确缩进:
class UsersController < ApplicationController
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to the Your App!"
redirect_to @user
else
render 'new'
end
def edit
@user = User.find(params[:id])
end
end
end
希望现在很明显。