我有一個問題,我試圖添加一個gravatar圖像到我的代碼從學習示例書第7章 該網站顯示正常,但我無法通過rspec規範測試,我得到以下錯誤:添加gravatar問題
1) UsersController should have the right title Failure/Error: get :show, :id => @user ActionController::RoutingError: No route matches {:id=>nil, :controller=>"users", :action=>"show"} # ./spec/controllers/users_controller_spec.rb:36:in `block (2 levels) in '
2) UsersController should include the user's name Failure/Error: get :show, :id => @user ActionController::RoutingError: No route matches {:id=>nil, :controller=>"users", :action=>"show"} # ./spec/controllers/users_controller_spec.rb:41:in `block (2 levels) in '
3) UsersController should have a profile image Failure/Error: get :show, :id => @user ActionController::RoutingError: No route matches {:id=>nil, :controller=>"users", :action=>"show"} # ./spec/controllers/users_controller_spec.rb:46:in `block (2 levels) in '
4) UsersController GET 'show' should be successful Failure/Error: get :show, :id => @user ActionView::Template::Error: undefined method
gravatar_image_tag' for #<#<Class:0x00000003dda0d8>:0x00000003dd31c0> # ./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/show.html.erb:7:in_app_views_users_show_html_erb___2751740854697998587_32401380__1353884467646085556' # ./spec/controllers/users_controller_spec.rb:13:in
block (3 levels) in '5) UsersController GET 'show' should find the right user Failure/Error: get :show, :id => @user ActionView::Template::Error: undefined method
gravatar_image_tag' for #<#<Class:0x00000003dda0d8>:0x00000002c7e140> # ./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/show.html.erb:7:in_app_views_users_show_html_erb___2751740854697998587_32401380__1353884467646085556' # ./spec/controllers/users_controller_spec.rb:18:in
block (3 levels) in '
爲了給你提供一些背景,我不小心添加的gravatar寶石錯誤的領域,但我沒有改回正確的區域
規格/控制器/ users_controller_spec.rb
it "should have the right title" do
get :show, :id => @user
response.should have_selector("title", :content => @user.name)
end
it "should include the user's name" do
get :show, :id => @user
response.should have_selectori("h1", :content => @user.name)
end
it "should have a profile image" do
get :show, :id => @user
response.should have_selector("h1>img", :class => "gravatar")
end
end
應用程序/控制器/ Users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@title = @user.name
end
應用程序/助手/ users_helper.rb
module UsersHelper
def gravatar_for(user, options = { :size => 50 })
gravatar_image_tag(user.email.downcase, :alt => user.name,
:class => 'gravatar',
:gravatar => options)
end
end
應用/視圖/用戶/ show.html.erb
<%= @user.name %>, <%= @user.email %>
<table class="profile" summary="Profile Information">
<tr>
<td class="main">
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</td>
<td class="sidebar round">
<strong>Name</strong> <%= @user.name %><br />
<strong>URL</strong> <%= link_to user_path(@user), @user %>
</td>
</tr>
</table>
我不認爲是這樣的話 – mercxi
低於軌控制檯還通過了代碼,如果它可以幫助 – mercxi
$軌控制檯 >>用戶= User.first >> user.update_attributes(:電子郵件=> 「[email protected]」, > :密碼=> 「foobar的」, > :password_confirmation => 「foobar的」) =>真 – mercxi