How to configure these CRUD controller actions for has_many_polymorphs and errors

I use the has_many_polymorphs plugin so that videos, themes and users can be sent to profiles. Therefore, there are many "showable_objects" in the profile, which can be videos, themes, and users. In addition, the user who created the "showable_object" will also be associated with it. I have already established model associations (below).

The way I want to create showable_object is so that the user can select another user from the autocomplete field on the resource display page. This resource is then associated with the profile of the selected user.

My question is: how do I configure my showable_objects controller? Also, if I want it to be sent via AJAX, will the jQuery AJAX request look like?

UPDATE:

Here are my model associations:

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

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

By the way, I also get 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 namespaced, be sure to specify it as :"module/showable_objects_users" instead.

It points to this line <% if video.owned_by? current_user %>and is associated with a call has_many_polymorphsin the user model.

0
source share
1 answer

, , , rails-3, , , has_many_polymorphs . , : has_many_polymorphs by jystewart 3.

0

Source: https://habr.com/ru/post/1786661/


All Articles