【问题标题】:adding a gravatar issue添加 gravatar 问题
【发布时间】:2011-06-28 01:34:10
【问题描述】:

我遇到了一个问题,我试图将 gravatar 图像添加到我的代码中,从通过示例书第 7 章学习 该网站显示正常,但我无法通过 rspec 规范测试,我收到以下错误:

1) UsersController 应该有正确的标题 失败/错误:获取 :show, :id => @user 动作控制器::路由错误: 没有路由匹配 {:id=>nil, :controller=>"users", :action=>"show"} # ./spec/controllers/users_controller_spec.rb:36:in `block (2 levels) in '

2) UsersController 应该包含用户名 失败/错误:获取 :show, :id => @user 动作控制器::路由错误: 没有路由匹配 {:id=>nil, :controller=>"users", :action=>"show"} # ./spec/controllers/users_controller_spec.rb:41:in `block (2 levels) in '

3) UsersController 应该有个人资料图片 失败/错误:获取 :show, :id => @user 动作控制器::路由错误: 没有路由匹配 {:id=>nil, :controller=>"users", :action=>"show"} # ./spec/controllers/users_controller_spec.rb:46:in `block (2 levels) in '

4) UsersController GET 'show' 应该是成功的 失败/错误:获取 :show, :id => @user 动作视图::模板::错误: 未定义的方法gravatar_image_tag' for #<#<Class:0x00000003dda0d8>:0x00000003dd31c0> # ./app/helpers/users_helper.rb:4:ingravatar_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:inblock (3 级) in '

5) UsersController GET 'show' 应该找到正确的用户 失败/错误:获取 :show, :id => @user 动作视图::模板::错误: 未定义的方法gravatar_image_tag' for #<#<Class:0x00000003dda0d8>:0x00000002c7e140> # ./app/helpers/users_helper.rb:4:ingravatar_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:inblock (3 个级别) in '

为了给你一点背景知识,我不小心把 gravatar gem 添加到了错误的区域,但我确实把它改回了正确的区域

spec/controllers/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

app/controllers/Users_controller.rb

class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@title = @user.name
end

app/helpers/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

app/views/users/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>

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    对于有相同错误的人:确保在文件 users_controller_spec.rb 行中:

          before(:each) do
            @user = Factory(:user)
          end
    

    放置在 render_views 之后,否则不会为每个方法创建用户。

    【讨论】:

      【解决方案2】:

      :id=>nil,也许用户没有登录?你需要使用

      检查它

      【讨论】:

      • 我不这么认为
      • $ rails console >> user = User.first >> user.update_attributes(:email => "example@railstutorial.org", ?> :password => "foobar", ?> :password_confirmation => "foobar") => 真的
      • 我如何使用该代码检查它,对不起,我是一个真正的初学者
      【解决方案3】:

      您的错误清楚地表明 :id =&gt; nil 不是 :action =&gt; :show 路由的预期情况。

      当您没有正确设置 @user... 我敢打赌,如果您将其中一个规格更改为:

      it "should have the right title" do
        @user.id.should_not be_blank
        get :show, :id => @user
        response.should have_selector("title", :content => @user.name)
      end
      

      它会在第一行失败 - 说“nil expected to not be blank”之类的话。

      解决这个问题的方法是确保@user 被用户正确初始化。

      正如@user861181 所指出的,这可以通过设置工厂在before(:each) 块中完成。或者(如果您使用固定装置),您可以使用:

        before(:each) do
          @user = users(:one)
        end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-19
        相关资源
        最近更新 更多