如何为has_many_polymorphs和错误设置这些CRUD控制器操作 [英] How to set up these CRUD controller actions for has_many_polymorphs and an error

查看:52
本文介绍了如何为has_many_polymorphs和错误设置这些CRUD控制器操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the has_many_polymorphs plugin so that videos, topics, and users can be posted to profiles. Therefore, a profile has many "showable_objects" which can be videos, topics, and users. Also, the user who creates the "showable_object" will also be associated with it. I've already set up the model associations (below).

我想要创建showable_object的方式是让用户从的自动完成字段中选择另一个用户资源的显示页面。然后,该资源与所选用户的个人资料相关联。

The way I want a showable_object to be created is for a user to select another user from an autocomplete field on the show page of the resource. Then that resource is associated with the profile of the selected user.

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

My question is how should I be setting up my showable_objects controller? Also, if I want it to be sent via AJAX, what will the AJAX jQuery request look like?

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迁移:

This is my ShowableObject migration:

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):

By the way I'm also getting this error from these associations (so this is actually a two part question :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.

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

It points to this line <% if video.owned_by? current_user %> and has to do with the has_many_polymorphs call in the user model.

推荐答案

我从来没有使用过它,只是做了一些研究,但是我注意到您已经用rails-3标记了您的问题,并且据我所知,has_many_polymorphs不适用于它。我发现以下分叉可能有助于解决该错误:jystewart的 has_many_polymorphs

I've never used it, just presently doing some research, but I noticed that you have tagged your question with rails-3 and to my understanding, has_many_polymorphs doesn't work with it. I have found the following fork which might help with the error: has_many_polymorphs by jystewart for rails 3.

这篇关于如何为has_many_polymorphs和错误设置这些CRUD控制器操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆