当我向will_paginate添加“order”参数时,Rspec抛出疯狂的错误?

时间:2012-12-02 13:59:34

标签: ruby-on-rails

我有用户帖子模型。我在用户节目视图中显示用户的帖子:

users_controller.rb:

  def show
    @user = User.find(params[:id])

    default_order = "created_at DESC"
    params[:order_by] ||= default_order 
    @posts = @user.posts.paginate(page: params[:page],
                                  per_page: 10,
                                  order: params[:order_by]) if signed_in?
    @posts = @user.posts.paginate(page: params[:page])
  end

用户/ show.html.erb:

<% if @user.posts.any? %>
  <h3>Posts (<%= @user.posts.count %>)</h3>

  <ul class="posts unstyled">
    <%= render @posts %>
  </ul>
  <%= will_paginate @posts %>
<% end %>

帖子在直播网站上显示正常。但是当我运行这个规范时:

user_pages_spec.rb:

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    let!(:p1) { FactoryGirl.create(:post, user: user, title: "Fo",
                                   content: "Foo") }
    let!(:p2) { FactoryGirl.create(:post, user: user, title: "Ba",
                                   content: "Bar") }

    before { visit user_path(user) }

    it { should have_selector('h1',    text: user.name) }
    it { should have_selector('title', text: user.name) }

    describe "posts" do
      it { should have_content(p1.title) }
      it { should have_content(p1.content) }
      it { should have_content(p2.title) }
      it { should have_content(p2.content) }
      it { should have_content(user.posts.count) }
    end
  end

我得到了这个疯狂的错误:

 6) User pages profile page posts 
      Failure/Error: it { should have_content(p1.title) }
        expected there to be content "Fo" in "Action Controller: Exception caught 
          body { background-color: #fff; color: #333; }
          body, p, ol, ul, td {
            font-family: helvetica, verdana, arial, sans-serif;
            font-size:   13px;      
            line-height: 18px;
          }
          pre {
            background-color: #eee;     
            padding: 10px;
            font-size: 11px;
            white-space: pre-wrap;
          }    
          a { color: #000; }
          a:visited { color: #666; }   
          a:hover { color: #fff; background-color:#000; } 

          ArgumentError in
            Users#show



          Showing /home/alex/rails/inkleak/app/views/users/show.html.erb where line #20 raised:
          'nil' is not an ActiveModel-compatible object that returns a valid partial path.

           Extracted source (around line #20):
           17: <h3>Posts (<%= @user.posts.count %>)

然后往前走......就像千行一样。

这很奇怪,因为如果我评论这些行,测试工作正常:

 #default_order = "created_at DESC"
    #params[:order_by] ||= default_order 

    #order: params[:order_by])

可能是什么问题?

修改

这是我的工厂:

factories.rb:

  factory :user do
    sequence(:name)  { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}   
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end

  factory :post do
    title "lorem"
    content "lorem ipsum"
    #tagging
    category
    user
    after(:build) do |post|
      post.tags << FactoryGirl.build(:tag)
    end
  end

1 个答案:

答案 0 :(得分:1)

您使用will_paginate错误地使用order

这就是你要做的事情:

@user.posts.paginate(page: params[:page], per_page: 10).order(params[:order_by])

Source