如何为has_many_polymorphs设置这些CRUD控制器操作并出现错误

时间:2011-04-24 05:08:47

标签: ruby-on-rails ruby ruby-on-rails-3 crud has-many-polymorphs

我正在使用has_many_polymorphs插件,以便可以将视频,主题和用户发布到个人资料中。因此,配置文件具有许多“showable_objects”,可以是视频,主题和用户。此外,创建“showable_object”的用户也将与之关联。我已经建立了模型关联(下面)。

我希望创建showable_object的方式是让用户从资源的显示页面上的自动完成字段中选择另一个用户。然后该资源与所选用户的配置文件相关联。

我的问题是我应该如何设置我的showable_objects控制器?另外,如果我想通过AJAX发送它,那么AJAX jQuery请求会是什么样的?

更新:

以下是我的模特协会:

class ShowableObject < ActiveRecord::Base
  belongs_to :showable_object, :polymorphic => true
  belongs_to :user
  belongs_to :profile
end

class Profile < ActiveRecord::Base
  has_many_polymorphs :showable_objects, :from => [:videos, :users, :topics, :video_votes],
                                         :dependent => :destroy
end

class User < ActiveRecord::Base
  has_many_polymorphs :showable_objects, :from => [:videos, :users, :topics, :video_votes],
                                         :dependent => :destroy
end

这是我的ShowableObject迁移:

class CreateShowableObjects < ActiveRecord::Migration
  def self.up
    create_table :showable_objects do |t|
      t.references :showable_object, :polymorphic => true
      t.references :profile
      t.references :user

      t.timestamps
    end
  end

  def self.down
    drop_table :showable_objects
  end
end

顺便说一下,我也从这些关联中得到这个错误(所以这实际上是一个两部分问题:P):

ActiveRecord::Associations::PolymorphicError in Videos#index

Showing /rubyprograms/dreamstill/app/views/videos/_video.html.erb where line #24 raised:

Could not find a valid class for :showable_objects_users (tried ShowableObjectsUser). If it's namespaced, be sure to specify it as :"module/showable_objects_users" instead.

它指向此行<% if video.owned_by? current_user %>,并与用户模型中的has_many_polymorphs调用有关。

1 个答案:

答案 0 :(得分:0)

我从未使用它,只是目前正在进行一些研究,但我注意到你用rails-3标记了你的问题,据我所知,has_many_polymorphs无法使用它。我找到了以下可能有助于解决错误的分支:has_many_polymorphs by jystewart for rails 3