2015-01-02 13 views
7

Tôi nhận được thông báo lỗi này:Đơn vị thử nghiệm một hiệp hội đa hình với đồ đạc

ERROR["test_profile_display", UsersProfileTest, 2.569931] test_profile_display#UsersProfileTest (2.57s) 
NoMethodError:   NoMethodError: undefined method `posts' for nil:NilClass 
     app/controllers/users_controller.rb:14:in `show' 
     test/integration/users_profile_test.rb:22:in `block in <class:UsersProfileTest>' 
    app/controllers/users_controller.rb:14:in `show' 
    test/integration/users_profile_test.rb:22:in `block in <class:UsersProfileTest>' 

Dường character trong UsersController trong dòng @posts = @user.character.posts.paginate(page: params[:page]) là con số không. Có một mối quan hệ đa hình giữa người dùng và ký tự. Tôi nghĩ tôi đã sử dụng cú pháp đúng trong đồ đạc nói với đường ray rằng Bazley dùng có một vật cố: Trong characters.yml:

character_bazley: 
    sociable: bazley (user) 

Làm thế nào để thuyết phục đường ray rằng cố người dùng không thực sự có một nhân vật thuộc với nó?

thử nghiệm/hội nhập/users_profile_test.rb:

require 'test_helper' 

class UsersProfileTest < ActionDispatch::IntegrationTest 
    include ApplicationHelper 

    def setup 
    @user = users(:bazley) 
    end 

    test "profile display" do 
    log_in_as(@user) 
    get user_path(@user) 
    assert_template 'users/show' 
    assert_select 'title', full_title(@user.name) 
    assert_select 'h1', text: @user.name 
    assert_select 'h1>img.gravatar' 
    assert_match @user.character.posts.count.to_s, response.body 
    assert_select 'div.pagination' 
    @user.character.posts.paginate(page: 1).each do |post| 
     assert_match post.content, response.body 
    end 
    end 
end # UsersProfileTest 

user.rb:

class User < ActiveRecord::Base 

    attr_accessor :remember_token, :activation_token, :reset_token 
    has_one :character, as: :sociable, dependent: :destroy 
    . 
    . 
end 

character.rb:

class Character < ActiveRecord::Base 

    belongs_to :sociable, polymorphic: true 
    has_many :posts, dependent: :destroy 
    validates :sociable_id, presence: true 
end # Character 

users.yml:

bazley: 
    name: Bazley 
    email: [email protected] 
    callsign: bazley 
    password_digest: <%= User.digest('password') %> 
    admin: true 
    activated: true 
    activated_at: <%= Time.zone.now %> 

characters.yml:

character_bazley: 
    sociable: bazley (user) 

Trong UsersController:

def show 
    @user = User.find_by_callsign(params[:callsign]) 
    @posts = @user.character.posts.paginate(page: params[:page]) # the line causing the error 
    @page_name = "user_page" 
    redirect_to root_url and return unless @user.activated 
end 

Trả lời

8

Thay đổi characters.yml

character_bazley: 
    sociable: bazley (User) 

thay vì

character_bazley: 
    sociable: bazley (user) 

N ote là User và không phải là user. Từ khóa được sử dụng tại đây User sẽ cho biết rằng sociable_type là của lớp học User.

Ref:

http://ruby-journal.com/rails/define-fixtures-with-polymorphic-association/ http://api.rubyonrails.org/v3.2.8/classes/ActiveRecord/Fixtures.html#label-Polymorphic+belongs_to

+0

Ôi trời ơi, một bức thư vốn ... Tôi là một khúc gỗ. Cảm ơn bạn. – Bazley

Các vấn đề liên quan