Ruby on rails 多态状态关联

Ruby on rails 多态状态关联,ruby-on-rails,polymorphism,polymorphic-associations,Ruby On Rails,Polymorphism,Polymorphic Associations,我一直在Rails 4中开发一个web应用程序,并使用Michael Hartl教程作为它的主干,但也使用Desive。在构建了其中的一些之后,我回去将状态模型更改为多态,以便允许用户、场馆和团体发布状态。到目前为止,我对这件事有一定的了解,但现在我被卡住了。尝试查看用户的配置文件时,我当前遇到以下错误: NoMethodError in Users#show Showing /home/app_name/app/views/users/show.html.erb where line #6

我一直在Rails 4中开发一个web应用程序,并使用Michael Hartl教程作为它的主干,但也使用Desive。在构建了其中的一些之后,我回去将状态模型更改为多态,以便允许用户、场馆和团体发布状态。到目前为止,我对这件事有一定的了解,但现在我被卡住了。尝试查看用户的配置文件时,我当前遇到以下错误:

NoMethodError in Users#show

Showing /home/app_name/app/views/users/show.html.erb where line #6 raised:

undefined method `avatar' for #<ActiveRecord::AssociationRelation::ActiveRecord_AssociationRelation_Status:0xb6859fd8>
Extracted source (around line #6):

<aside class="col-md-4">
  <section>
    <h1>
        <%= image_tag @user.avatar.url(:thumb) %>
    <%= @user.name %>
    </h1>
  </section>
为了便于阅读,我删去了一些内容,但希望能留下所需的内容

编辑

class UsersController < ApplicationController

before_filter :authenticate_user!, only: [:index, :show,:edit,
:update, :destroy, :favourite_users, :favourited_users]

def index

 @users = User.all_except(current_user).paginate(page: params[:page]).order('created_at DESC')

end


def show

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

   @user = @user.statuses.paginate(page: params[:page])

end


def new

 @user = User.new

end


def edit

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

end


def update

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

    if @user.update_attributes(user_params)

     # Handle a successful update.

    else

     render 'edit'

    end

end


def create

 @user = User.create( user_params )

end


def favourite_users

 @title = "Favourites"

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

 @users = @user.favourite_users.paginate(page: params[:page])

 render 'show_favourite'

end


def favourited_users

 @title = "Listed as a Favourite by"

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

 @users = @user.favourited_users.paginate(page: params[:page])

 render 'show_favourite'

end
class UsersController
嗯,它看起来确实像
@user
不包含
用户
,而是包含
ActiveRecord::AssociationRelation::ActiveRecord\u AssociationRelation\u状态
。难怪它没有
avatar
方法!检查控制器代码以查看
@user
实际设置为什么。如果你不明白发生了什么,把控制器贴在这里。
用户
模型和模式可能也需要。

你能发布你的模型吗?谢谢,我看过了,但还是很迷茫。我现在已经添加了模型、控制器和模式,看看是否有其他人可以解决它!谢谢抱歉,应该要求您包括
用户\u控制器
,因为这是引发错误的视图。我现在已添加了用户控制器。我猜问题可能来自Show方法中的will_paginate行?是的!这条线断了。在一天左右的时间里都无法查看详细信息。成功了!谢谢你的帮助。不确定我最初为什么会偏离Michael Hartl教程,但will_paginate部分应该是
@user=@user.statuses.paginate(page:params[:page])
,在视图中,我将其写成
will_paginate@user.statuses
,而不是
@statuses
。谢谢你的帮助!
    class StatusController < ApplicationController
  before_action :authenticate_user!, only: [:create, :destroy]
  before_filter :load_statusable

  def new
    @status = Status.new(status_params)
  end


  def create
    @statusable = load_statusable
    @status = @statusable.statuses.build(status_params)
    if @status.save
        flash[:success] = "Status created!"
        redirect_to root_url
    else
      @feed_items = []
        render 'static_pages/home'
    end
  end

  def destroy
    @status.destroy
    redirect_to root_url
  end

  private

    def status_params
        params.require(:status).permit(:content)
    end

    def load_statusable
      resource, id = request.path.split('/')[1, 2]
      resource_name = resource.singularize.classify
      if resource_name = "user"
        @statusable = current_user
      else 
        @statusable = resource_name.constantize.find(id)
      end
    end
end
create_table "statuses", force: true do |t|
    t.string   "content"
    t.integer  "statusable_id"
    t.string   "statusable_type"
    t.float    "latitude"
    t.float    "longitude"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
class UsersController < ApplicationController

before_filter :authenticate_user!, only: [:index, :show,:edit,
:update, :destroy, :favourite_users, :favourited_users]

def index

 @users = User.all_except(current_user).paginate(page: params[:page]).order('created_at DESC')

end


def show

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

   @user = @user.statuses.paginate(page: params[:page])

end


def new

 @user = User.new

end


def edit

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

end


def update

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

    if @user.update_attributes(user_params)

     # Handle a successful update.

    else

     render 'edit'

    end

end


def create

 @user = User.create( user_params )

end


def favourite_users

 @title = "Favourites"

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

 @users = @user.favourite_users.paginate(page: params[:page])

 render 'show_favourite'

end


def favourited_users

 @title = "Listed as a Favourite by"

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

 @users = @user.favourited_users.paginate(page: params[:page])

 render 'show_favourite'

end