1

我有一个用户和一个帖子模型。我在用户显示视图中显示用户的帖子:

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])

可能是什么问题呢?

编辑:

这是我的工厂:

工厂.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
4

1 回答 1

1

order对 will_paginate 的使用不正确。

这就是你的做法:

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

来源

于 2012-12-02T17:32:28.567 回答