Ruby on rails 9.1.3之前的Hartl章节错误
我正在阅读Hartl先生的教程,并试图准确地复制它,甚至有点使用他完整的github代码作为指导,但在运行“bundle exec rspec spec/”之后,我仍然坚持以下几点: 失败:Ruby on rails 9.1.3之前的Hartl章节错误,ruby-on-rails,ruby,rspec,bundle,Ruby On Rails,Ruby,Rspec,Bundle,我正在阅读Hartl先生的教程,并试图准确地复制它,甚至有点使用他完整的github代码作为指导,但在运行“bundle exec rspec spec/”之后,我仍然坚持以下几点: 失败: 1) Authentication signin with valid information Failure/Error: before { sign_in user } ActionView::MissingTemplate: Missing template s
1) Authentication signin with valid information
Failure/Error: before { sign_in user }
ActionView::MissingTemplate:
Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/home/nemo/rails_projects/sample_app/app/views"
# ./spec/support/utilities.rb:19:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:32:in `block (4 levels) in <top (required)>'
2) Authentication signin with valid information
Failure/Error: before { sign_in user }
ActionView::MissingTemplate:
Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/home/nemo/rails_projects/sample_app/app/views"
# ./spec/support/utilities.rb:19:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:32:in `block (4 levels) in <top (required)>'
3) Authentication signin with valid information
Failure/Error: before { sign_in user }
ActionView::MissingTemplate:
Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/home/nemo/rails_projects/sample_app/app/views"
# ./spec/support/utilities.rb:19:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:32:in `block (4 levels) in <top (required)>'
4) Authentication signin with valid information
Failure/Error: before { sign_in user }
ActionView::MissingTemplate:
Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/home/nemo/rails_projects/sample_app/app/views"
# ./spec/support/utilities.rb:19:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:32:in `block (4 levels) in <top (required)>'
5) Authentication signin with valid information
Failure/Error: before { sign_in user }
ActionView::MissingTemplate:
Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/home/nemo/rails_projects/sample_app/app/views"
# ./spec/support/utilities.rb:19:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:32:in `block (4 levels) in <top (required)>'
6) Authentication signin with valid information followed by signout
Failure/Error: before { sign_in user }
ActionView::MissingTemplate:
Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/home/nemo/rails_projects/sample_app/app/views"
# ./spec/support/utilities.rb:19:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:32:in `block (4 levels) in <top (required)>'
7) User pages profile page signup with valid information after saving the user
Failure/Error: before { visit user_path(user) }
AbstractController::ActionNotFound:
The action 'index' could not be found for UsersController
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
8) User pages profile page signup with valid information after saving the user
Failure/Error: before { visit user_path(user) }
AbstractController::ActionNotFound:
The action 'index' could not be found for UsersController
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
9) User pages profile page signup with valid information after saving the user
Failure/Error: before { visit user_path(user) }
AbstractController::ActionNotFound:
The action 'index' could not be found for UsersController
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
10) User pages profile page with invalid information
Failure/Error: before { click_button "Save changes" }
Capybara::ElementNotFound:
Unable to find button "Save changes"
# ./spec/requests/user_pages_spec.rb:76:in `block (4 levels) in <top (required)>'
11) User pages profile page page
Failure/Error: it { should have_content("Update your profile") }
expected #has_content?("Update your profile") to return true, got false
# ./spec/requests/user_pages_spec.rb:70:in `block (4 levels) in <top (required)>'
12) User pages profile page page
Failure/Error: it { should have_title("Edit user") }
expected #has_title?("Edit user") to return true, got false
# ./spec/requests/user_pages_spec.rb:71:in `block (4 levels) in <top (required)>'
13) User pages profile page page
Failure/Error: it { should have_link('change', href: 'http://gravatar.com/emails') }
expected #has_link?("change", {:href=>"http://gravatar.com/emails"}) to return true, got false
# ./spec/requests/user_pages_spec.rb:72:in `block (4 levels) in <top (required)>'
Finished in 1.83 seconds
53 examples, 13 failures
规格/请求/用户\u页面\u规格.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
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
describe "after submission" do
before { click_button submit }
it { should have_title('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 { 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_link('Sign out') }
it { should have_title(user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
end
end
end
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before { visit edit_user_path(user) }
end
describe "page" do
it { should have_content("Update your profile") }
it { should have_title("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
app/controllers/users\u controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
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(user_params)
# Handle a successful update.
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
# Sign the user in and redirect to the user's show page.
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
sign_out
redirect_to root_url
end
end
添加了app/controllers/sessions\u controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
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(user_params)
# Handle a successful update.
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
# Sign the user in and redirect to the user's show page.
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
sign_out
redirect_to root_url
end
end
这次它只导致了7个错误
Failures:
1) User pages profile page signup with valid information after saving the user
Failure/Error: before { visit user_path(user) }
NoMethodError:
undefined method `paginate' for #<Class:0xa6de278>
# ./app/controllers/users_controller.rb:4:in `index'
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
2) User pages profile page signup with valid information after saving the user
Failure/Error: before { visit user_path(user) }
NoMethodError:
undefined method `paginate' for #<Class:0xa6de278>
# ./app/controllers/users_controller.rb:4:in `index'
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
3) User pages profile page signup with valid information after saving the user
Failure/Error: before { visit user_path(user) }
NoMethodError:
undefined method `paginate' for #<Class:0xa6de278>
# ./app/controllers/users_controller.rb:4:in `index'
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
4) User pages profile page with invalid information
Failure/Error: before { click_button "Save changes" }
Capybara::ElementNotFound:
Unable to find button "Save changes"
# ./spec/requests/user_pages_spec.rb:76:in `block (4 levels) in <top (required)>'
5) User pages profile page page
Failure/Error: it { should have_content("Update your profile") }
expected #has_content?("Update your profile") to return true, got false
# ./spec/requests/user_pages_spec.rb:70:in `block (4 levels) in <top (required)>'
6) User pages profile page page
Failure/Error: it { should have_title("Edit user") }
expected #has_title?("Edit user") to return true, got false
# ./spec/requests/user_pages_spec.rb:71:in `block (4 levels) in <top (required)>'
7) User pages profile page page
Failure/Error: it { should have_link('change', href: 'http://gravatar.com/emails') }
expected #has_link?("change", {:href=>"http://gravatar.com/emails"}) to return true, got false
# ./spec/requests/user_pages_spec.rb:72:in `block (4 levels) in <top (required)>'
Finished in 1.88 seconds
53 examples, 7 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:57 # User pages profile page signup with valid information after saving the user
rspec ./spec/requests/user_pages_spec.rb:58 # User pages profile page signup with valid information after saving the user
rspec ./spec/requests/user_pages_spec.rb:59 # User pages profile page signup with valid information after saving the user
rspec ./spec/requests/user_pages_spec.rb:78 # User pages profile page with invalid information
rspec ./spec/requests/user_pages_spec.rb:70 # User pages profile page page
rspec ./spec/requests/user_pages_spec.rb:71 # User pages profile page page
rspec ./spec/requests/user_pages_spec.rb:72 # User pages profile page page
故障:
1) 保存用户后,使用有效信息注册用户页面配置文件页面
失败/错误:{访问用户\路径(用户)}之前
命名错误:
未定义的方法“paginate”#
#./app/controllers/users\u controller.rb:4:in'index'
#./spec/requests/user\u pages\u spec.rb:16:in'block(3级)in'
2) 保存用户后,使用有效信息注册用户页面配置文件页面
失败/错误:{访问用户\路径(用户)}之前
命名错误:
未定义的方法“paginate”#
#./app/controllers/users\u controller.rb:4:in'index'
#./spec/requests/user\u pages\u spec.rb:16:in'block(3级)in'
3) 保存用户后,使用有效信息注册用户页面配置文件页面
失败/错误:{访问用户\路径(用户)}之前
命名错误:
未定义的方法“paginate”#
#./app/controllers/users\u controller.rb:4:in'index'
#./spec/requests/user\u pages\u spec.rb:16:in'block(3级)in'
4) 包含无效信息的用户页面配置文件页面
失败/错误:{单击按钮“保存更改”之前}
Capybara::ElementNotFound:
找不到“保存更改”按钮
#./spec/requests/user\u pages\u spec.rb:76:in'block(4级)in'
5) 用户页面配置文件页面
失败/错误:它{应该有_内容(“更新您的配置文件”)}
期望#has_content?(“更新您的个人资料”)返回true,得到false
#./spec/requests/user\u pages\u spec.rb:70:in'block(4级)in'
6) 用户页面配置文件页面
失败/错误:它{应该有_标题(“编辑用户”)}
期望#has_title?(“编辑用户”)返回true,得到false
#./spec/requests/user\u pages\u spec.rb:71:in'block(4级)in'
7) 用户页面配置文件页面
失败/错误:它{应该有_链接('change',href:'http://gravatar.com/emails') }
应为#has_link?(“更改”,{:href=>”http://gravatar.com/emails“})要返回true,则返回false
#./spec/requests/user\u pages\u spec.rb:72:in'block(4级)in'
以1.88秒完成
53例,7例失败
失败的示例:
rspec./spec/requests/user_pages_spec.rb:57#用户页面配置文件页面保存用户后使用有效信息注册
rspec./spec/requests/user_pages_spec.rb:58#用户页面配置文件页面保存用户后使用有效信息注册
rspec./spec/requests/user_pages_spec.rb:59#用户页面配置文件页面保存用户后使用有效信息注册
rspec./spec/requests/user_pages_spec.rb:78#用户页面配置文件页面包含无效信息
rspec./spec/requests/user_pages_spec.rb:70#user pages profile page
rspec./spec/requests/user_pages_spec.rb:71#user pages profile page
rspec./spec/requests/user_pages_spec.rb:72#user pages profile page
问题似乎出在您的会话#create
方法上:
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
# Sign the user in and redirect to the user's show page.
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
我怀疑你是在有注释的地方结束的,在这一点上它“通过”Rails标准控制器代码,然后它会查找与此相对应的create
sessions视图,这就是create
controller方法
如果您在Hartl github存储库中签出相应的代码,则其:
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
sign_in user
redirect_back_or user
end
end
至于您是如何到达那里的,似乎
登录用户
方法成功地进入并通过您的会话新建
控制器和查看代码。您眼中的submit
将您带到会话#创建点。好吧,如果您查看第一组错误,它们都发生在登录用户
,因此共享该代码会很有帮助。谢谢,我包括了utilities.rb,如果这是相关代码的话。好的,看来问题在于访问signin_path
,因此查看(或共享)您的路由(例如使用rake routes
)并共享获取signin_path
方法的代码对于rake routes,我获取signin GET/signin(:格式)sessions#new
我不确定在这种情况下,code
是什么,但看起来您需要共享您的sessions\u控制器。rb
文件我知道这是违反直觉的,但Hartl确实在本章后面的9.19中将代码更改为底部代码,但在改变一系列其他代码以适应这种变化之前。我知道这是一个理想的代码,但它可能还不起作用,我只是在努力按照正确的顺序阅读教程。我插入了新代码,出现了16个错误。谢谢您的帮助。至少在当前版本的教程中,您缺少的代码在清单8.13的第8.2节中介绍了,它还原了早期Hartl中的一些代码,只出现了7个错误。您看到我之前关于清单8.13的评论了吗?您一直都在遵循教程并传递吗?
Failures:
1) User pages profile page signup with valid information after saving the user
Failure/Error: before { visit user_path(user) }
NoMethodError:
undefined method `paginate' for #<Class:0xa6de278>
# ./app/controllers/users_controller.rb:4:in `index'
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
2) User pages profile page signup with valid information after saving the user
Failure/Error: before { visit user_path(user) }
NoMethodError:
undefined method `paginate' for #<Class:0xa6de278>
# ./app/controllers/users_controller.rb:4:in `index'
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
3) User pages profile page signup with valid information after saving the user
Failure/Error: before { visit user_path(user) }
NoMethodError:
undefined method `paginate' for #<Class:0xa6de278>
# ./app/controllers/users_controller.rb:4:in `index'
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
4) User pages profile page with invalid information
Failure/Error: before { click_button "Save changes" }
Capybara::ElementNotFound:
Unable to find button "Save changes"
# ./spec/requests/user_pages_spec.rb:76:in `block (4 levels) in <top (required)>'
5) User pages profile page page
Failure/Error: it { should have_content("Update your profile") }
expected #has_content?("Update your profile") to return true, got false
# ./spec/requests/user_pages_spec.rb:70:in `block (4 levels) in <top (required)>'
6) User pages profile page page
Failure/Error: it { should have_title("Edit user") }
expected #has_title?("Edit user") to return true, got false
# ./spec/requests/user_pages_spec.rb:71:in `block (4 levels) in <top (required)>'
7) User pages profile page page
Failure/Error: it { should have_link('change', href: 'http://gravatar.com/emails') }
expected #has_link?("change", {:href=>"http://gravatar.com/emails"}) to return true, got false
# ./spec/requests/user_pages_spec.rb:72:in `block (4 levels) in <top (required)>'
Finished in 1.88 seconds
53 examples, 7 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:57 # User pages profile page signup with valid information after saving the user
rspec ./spec/requests/user_pages_spec.rb:58 # User pages profile page signup with valid information after saving the user
rspec ./spec/requests/user_pages_spec.rb:59 # User pages profile page signup with valid information after saving the user
rspec ./spec/requests/user_pages_spec.rb:78 # User pages profile page with invalid information
rspec ./spec/requests/user_pages_spec.rb:70 # User pages profile page page
rspec ./spec/requests/user_pages_spec.rb:71 # User pages profile page page
rspec ./spec/requests/user_pages_spec.rb:72 # User pages profile page page
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
# Sign the user in and redirect to the user's show page.
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
sign_in user
redirect_back_or user
end
end