【问题标题】:How to set up these CRUD controller actions for has_many_polymorphs and an error如何为 has_many_polymorphs 和错误设置这些 CRUD 控制器操作
【发布时间】:2011-04-24 05:08:46
【问题描述】:

我正在使用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.

它指向这条线 &lt;% if video.owned_by? current_user %&gt; 并且与用户模型中的 has_many_polymorphs 调用有关。

【问题讨论】:

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


    【解决方案1】:

    我从未使用过它,只是目前正在做一些研究,但我注意到您已经用 rails-3 标记了您的问题,据我了解,has_many_polymorphs 不适用于它。我发现了以下可能有助于解决错误的 fork:has_many_polymorphs by jystewart for rails 3

    【讨论】:

      猜你喜欢
      • 2010-12-06
      • 2022-12-14
      • 1970-01-01
      • 2011-09-05
      • 2021-06-01
      • 1970-01-01
      • 2011-09-22
      • 2013-06-01
      • 2012-02-25
      相关资源
      最近更新 更多