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

2024-03-24

我正在使用有很多多态性 https://github.com/fauna/has_many_polymorphs插件,以便可以将视频、主题和用户发布到个人资料中。因此,一个个人资料有很多“showable_objects”,可以是视频、主题和用户。此外,创建“showable_object”的用户也将与其关联。我已经建立了模型关联(如下)。

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

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

UPDATE:

这是我的模型协会:

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调用用户模型。


我从未使用过它,只是目前正在做一些研究,但我注意到您已用 Rails-3 标记了您的问题,据我了解, has_many_polymorphs 不适用于它。我发现以下分支可能有助于解决该错误:has_many_polymorphs 由 jystewart 为 Rails 3 设计 https://github.com/jystewart/has_many_polymorphs.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何为 has_many_polymorphs 和错误设置这些 CRUD 控制器操作 的相关文章

随机推荐