gpt4 book ai didi

ruby-on-rails-4 - hartl 教程在 9.2.1 结束时未通过测试

转载 作者:行者123 更新时间:2023-12-04 02:49:30 25 4
gpt4 key购买 nike

我正在研究 Hartl 的 Rails 教程,第 9.2.1 节结束。我的测试没有通过。不知道为什么。希望有人能提供帮助。我已在下面附上错误消息和两个相关文件 1) authentication_pages_spec.rb 和 2) users_controller.rb

失败:

1) 访问编辑页面的Users Controller 中对未登录用户的鉴权授权 失败/错误:它 { should have_title('Sign in') } 预期 #has_title?("Sign in") 返回 true,结果为 false # ./spec/requests/authentication_pages_spec.rb:57:in `block (6 levels) in '

2) 提交更新 Action 的Users Controller 中对未登录用户的鉴权授权 失败/错误:在 { patch user_path(user) } 之前 Action Controller ::参数缺失: 未找到参数:用户 # ./app/controllers/users_controller.rb:40:in user_params'
# ./app/controllers/users_controller.rb:27:in
update' # ./spec/requests/authentication_pages_spec.rb:61:in `block (6 levels) in '

在 2.08 秒内完成60 个例子,2 个失败

失败的例子:

rspec ./spec/requests/authentication_pages_spec.rb:57 # 访问编辑页面的Users Controller 中非登录用户的认证授权rspec ./spec/requests/authentication_pages_spec.rb:62 # 提交更新操作的用户 Controller 中非登录用户的身份验证授权

我的 authentication_pages_spec.rb 是

require 'spec_helper'

describe "Authentication" do

subject { page }


describe "signin page" do
before { visit signin_path }

it { should have_content('Sign in') }
it { should have_title('Sign in') }
end


describe "signin" do
before { visit signin_path }

describe "with invalid information" do
before { click_button "Sign in" }

it { should have_title('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 { sign_in user }

it { should have_title(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

describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }

describe "in the Users controller" do

describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_title('Sign in') }
end

describe "submitting to the update action" do
before { patch user_path(user) }
specify { expect(response).to redirect_to(signin_path) }
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(user_params)
if @user.save
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)
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end


private

def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end

# Before filters

def signed_in_user
redirect_to signin_url, notice: "Please sign in." unless signed_in?
end
end

最佳答案

您在 users_controller.rb 的第 2 行缺少对 signed_in_user 过滤器的调用。你在 Controller 的末尾定义了过滤器,但你没有调用它! :)

在本教程的最后,该行应如下所示:

before_action :signed_in_user, only: [:index, :edit, :update, :destroy, :following, :followers]

但是由于您还没有创建关注者,我认为它应该看起来像这样:

before_action :signed_in_user, only: [:index, :edit, :update, :destroy]

我发现将代码的最终源代码复制到我的硬盘上非常有用,因此我在编写本书时遇到错误时可以将我的代码与原始代码进行比较。

关于ruby-on-rails-4 - hartl 教程在 9.2.1 结束时未通过测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18059635/

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