【问题标题】:Rails 3 - Using tags to associate photos with polymorphic Taggable classRails 3 - 使用标签将照片与多态可标记类相关联
【发布时间】:2011-12-21 23:04:35
【问题描述】:

我的目标是创建一个系统,用于使用标签将照片与来自多个类别(事件、组织、发展)的对象关联起来。就我的一生而言,我无法解决这个问题,尽管这似乎是一种很常见的情况。

除了最基本的 Rails 开发之外,我对任何东西都比较陌生,所以我很难形成这个问题。请原谅任何用词不当。

标签模型:

class Tag < ActiveRecord::Base
  attr_accessible :photo_id, :taggable_id, :taggable_type

  belongs_to :photo
  belongs_to :taggable, :polymorphic => true
end

照片模型:

class Photo < ActiveRecord::Base
  attr_accessible :tags_attributes
  has_many :tags, :dependent => :destroy
  accepts_nested_attributes_for :tags, :reject_if => lambda { |a| a[:taggable_id].blank? }
end

事件模型:

class Event < ActiveRecord::Base
  has_many :tags, :as => :taggable, :dependent => :destroy
end

组织模式:

class Organization < ActiveRecord::Base
  has_many :tags, :as => :taggable, :dependent => :destroy
end

开发模式:

class Development < ActiveRecord::Base
  has_many :tags, :as => :taggable, :dependent => :destroy
end

在我的照片字段中,我正在尝试使用 nested_form gem 为照片添加标签(这样我以后可以在照片视图中调用这些标记对象,并在标记对象视图中调用照片)。

photos/new.html.erb(我已经包含了nested_form javascript)

<% nested_form_for @photo, :html => { :multipart => true } do |f| %>
  ...
  <%= f.fields_for :tags do |tag_form| %>
    <%= tag_form.collection_select :taggable_id, Taggable.all, :id, :name %>
    <%= tag_form.link_to_remove "remove" %>
  <% end %>
  <p><%= f.link_to_add "Add tag", :tags %></p>
  ...
  <% f.submit "Add photo" %>
<% end %>

我的模型结构是否适合我想要做的事情?

如果是这样,

如何在嵌套表单中正确指定 he :taggable_id 和 :taggable_type?

提前感谢您的指导!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 tagging polymorphic-associations


    【解决方案1】:

    您需要使您的照片模型多态并使用附件_fu/回形针之类的东西来上传照片。

    class Photo < ActiveRecord::Base
    
      has_attachment :content_type => :image,
                     :storage => :file_system,
                     :max_size => 2000.kilobytes,
                     :resize_to => '500x500>',
                     :thumbnails => { :thumb => '215x215>'}
    
      validates_as_attachment
    
      belongs_to :attachable, :polymorphic => true
    end
    
    class Event < ActiveRecord::Base
      has_many :tags, :as => :taggable, :dependent => :destroy
      has_many :attachments, :as => :attachable
    end
    
    class Organization < ActiveRecord::Base
      has_many :tags, :as => :taggable, :dependent => :destroy
      has_many :attachments, :as => :attachable
    end
    
    class Development < ActiveRecord::Base
      has_many :tags, :as => :taggable, :dependent => :destroy
      has_many :attachments, :as => :attachable
    end
    

    您的照片表应该有 attachable_id 和 attachable_type 列。有了这个,您应该能够将照片添加到所有存储在多态照片模型中的不同对象。标记也使用多态关联并以类似的方式工作。该插件的文档会有所帮助。希望对你有用。

    【讨论】:

    • 标签在哪里发挥作用?除非我弄错了(一种明显的可能性),否则这似乎可以独立地处理标签和附件。我正在寻找更像has_many :attachments, :through =&gt; :tags 的东西(不确定我的语法是否正确,但很接近)。让我直截了当,如果可以的话……谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多