So as the title says, I'm following Michael Hartl's RoR Book and I'm currently on chapter 9.3 - Showing User. All tests passed before this chapter, I'm following the Rails 4 version of the book and I'm very new to this RoR.
正如标题所说,我正在关注Michael Hartl的RoR Book,我目前正在进行第9.3章 - 显示用户。所有测试都在本章之前通过,我正在阅读本书的Rails 4版本,我对这个RoR很新。
I did bundle exec rspec spec/ and got this errors
我做了捆绑exec rspec spec /并得到了这个错误
1) Authentication authorization for non-signed-in users in the Users controller visiting the user index Failure/Error: it { should have_title('Sign in') } expected #has_title?("Sign in") to return true, got false # ./spec/requests/authentication_pages_spec.rb:80:in `block (6 levels) in '
1)访问用户索引的Users控制器中的非登录用户的身份验证授权失败/错误:它{should have_title('登录')}期望#has_title?(“登录”)返回true,得到false #./spec/requests/authentication_pages_spec.rb:80:在'块(6级)中'
2) User pages index Failure/Error: visit users_path ActionView::Template::Error: wrong number of arguments (2 for 1) # ./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/index.html.erb:7:in
block in _app_views_users_index_html_erb__3697129218785207645_26541860' # ./app/views/users/index.html.erb:5:in_app_views_users_index_html_erb__3697129218785207645_26541860' # ./spec/requests/user_pages_spec.rb:12:in
block (3 levels) in '2)用户页面索引失败/错误:访问users_path ActionView :: Template ::错误:参数数量错误(2表示1)#。/ app / help / users_helper.rb:4:in gravatar_for'#。/ app / views /users/index.html.erb:7:inblock in _app_views_users_index_html_erb__3697129218785207645_26541860'#。/ app / views / users / index.html.erb:5:in _app_views_users_index_html_erb__3697129218785207645_26541860'#。/ spec / requests / user_pages_spec.rb:12:inblock( 3个级别)'
3) User pages index Failure/Error: visit users_path ActionView::Template::Error: wrong number of arguments (2 for 1) # ./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/index.html.erb:7:in
block in _app_views_users_index_html_erb__3697129218785207645_26541860' # ./app/views/users/index.html.erb:5:in_app_views_users_index_html_erb__3697129218785207645_26541860' # ./spec/requests/user_pages_spec.rb:12:in
block (3 levels) in '3)用户页面索引失败/错误:访问users_path ActionView :: Template ::错误:参数数量错误(2表示1)#。/ app / help / users_helper.rb:4:in gravatar_for'#。/ app / views /users/index.html.erb:7:inblock in _app_views_users_index_html_erb__3697129218785207645_26541860'#。/ app / views / users / index.html.erb:5:in _app_views_users_index_html_erb__3697129218785207645_26541860'#。/ spec / requests / user_pages_spec.rb:12:inblock( 3个级别)'
4) User pages index should list each user Failure/Error: visit users_path ActionView::Template::Error: wrong number of arguments (2 for 1) # ./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/index.html.erb:7:in
block in _app_views_users_index_html_erb__3697129218785207645_26541860' # ./app/views/users/index.html.erb:5:in_app_views_users_index_html_erb__3697129218785207645_26541860' # ./spec/requests/user_pages_spec.rb:12:in
block (3 levels) in '4)用户页面索引应列出每个用户失败/错误:访问users_path ActionView :: Template ::错误:参数数量错误(2表示1)#。/ app / help / users_helper.rb:4:in gravatar_for'#。 /app/views/users/index.html.erb:7:inblock in _app_views_users_index_html_erb__3697129218785207645_26541860'#./app/views/users/index.html.erb:5:in _app_views_users_index_html_erb__3697129218785207645_26541860'#./spec/requests/user_pages_spec.rb: 12:inblock(3级)in'
Finished in 3.16 seconds 66 examples, 4 failures
完成3.16秒66例,4次失败
Failed examples:
失败的例子:
rspec ./spec/requests/authentication_pages_spec.rb:80 # Authentication authorization for non-signed-in users in the Users controller visiting the user index rspec ./spec/requests/user_pages_spec.rb:16 # User pages index rspec ./spec/requests/user_pages_spec.rb:15 # User pages index rspec ./spec/requests/user_pages_spec.rb:18 # User pages index should list each user
rspec ./spec/requests/authentication_pages_spec.rb:80#用户控制器中访问用户索引的非登录用户的身份验证授权rspec ./spec/requests/user_pages_spec.rb:16#用户页面索引rspec ./spec /requests/user_pages_spec.rb:15#用户页面索引rspec ./spec/requests/user_pages_spec.rb:18#用户页面索引应列出每个用户
Randomized with seed 40709
随机种子40709
Here's my authentication_pages_spec.rb
这是我的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('Users', href: users_path) }
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 "when attempting to visit a protected page" do
before do
visit edit_user_path(user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
describe "after signing in" do
it "should render the desired protected page" do
expect(page).to have_title('Edit user')
end
end
end
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
describe "visiting the user index" do
before { visit users_path }
it { should have_title('Sign in') }
end
end
end
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
before { sign_in user, no_capybara: true }
describe "submitting a GET request to the Users#edit action" do
before { get edit_user_path(wrong_user) }
specify { expect(response.body).not_to match(full_title('Edit user')) }
specify { expect(response).to redirect_to(root_url) }
end
describe "submitting a PATCH request to the Users#update action" do
before { patch user_path(wrong_user) }
specify { expect(response).to redirect_to(root_url) }
end
end
end
end
And Here's my users_controller.rb
这是我的users_controller.rb
class UsersController < ApplicationController
before_action :signed_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
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"
redirect_to @user
else
render 'edit'
end
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
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before filters
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
end
And here's my user_pages_spec.rb
这是我的user_pages_spec.rb
class UsersController < ApplicationController
before_action :signed_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]
def index
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
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"
redirect_to @user
else
render 'edit'
end
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
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before filters
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
end
I really have no idea what went wrong, please point me to the right direction.
我真的不知道出了什么问题,请指出正确的方向。
2 个解决方案
#1
0
This looks like a template error.
这看起来像模板错误。
One error points towards this file: /app/helpers/users_helper.rb line 4 and it looks like you have the wrong number of arguments in gravatar_for
. Then the next place to look is /app/views/users/index.html.erb line 7.
一个错误指向此文件:/app/helpers/users_helper.rb第4行,看起来你在gravatar_for中有错误的参数数量。接下来要看的是/app/views/users/index.html.erb第7行。
#2
0
I figured it out.
我想到了。
The problem is with the gravatar_for arguments.
问题在于gravatar_for参数。
I made mistake and did not update the gravatar_for method in users_helper that's what is giving this error.
我犯了错误,并且没有更新users_helper中的gravatar_for方法,这就是给出此错误的原因。
def gravatar_for(user, options = { size: 50 })
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
Updated gravatar_for method.
更新了gravatar_for方法。
#1
0
This looks like a template error.
这看起来像模板错误。
One error points towards this file: /app/helpers/users_helper.rb line 4 and it looks like you have the wrong number of arguments in gravatar_for
. Then the next place to look is /app/views/users/index.html.erb line 7.
一个错误指向此文件:/app/helpers/users_helper.rb第4行,看起来你在gravatar_for中有错误的参数数量。接下来要看的是/app/views/users/index.html.erb第7行。
#2
0
I figured it out.
我想到了。
The problem is with the gravatar_for arguments.
问题在于gravatar_for参数。
I made mistake and did not update the gravatar_for method in users_helper that's what is giving this error.
我犯了错误,并且没有更新users_helper中的gravatar_for方法,这就是给出此错误的原因。
def gravatar_for(user, options = { size: 50 })
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
Updated gravatar_for method.
更新了gravatar_for方法。