[英]rails 3 polymorphic association with paperclip and multiple models
I want to make polymorphic associations with paperclip, and allow my user to have one avatar and multiple images. 我想与paperclip建立多态关联,并允许我的用户拥有一个头像和多个图像。
Attachment model: 附件模型:
class Attachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
end
class Avatar < Attachment
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end
class Image < Attachment
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end
User Model: 用户模型:
has_one :avatar, :as => :attachable, :class_name => 'Attachment', :conditions => {:type => 'avatar'}
accepts_nested_attributes_for :avatar
User Controller: 用户控制器:
def edit
@user.build_avatar
end
User View form: 用户视图表单:
<%= form_for @user, :html => { :multipart => true } do |f| %>
<%= f.fields_for :avatar do |asset| %>
<% if asset.object.new_record? %>
<%= asset.file_field :image %>
<% end %>
<% end %>
when I attempt to save the changes I get the error => unknown attribute: avatar 当我尝试保存更改时,我得到错误=>未知属性:头像
if I remove the :class_name => 'attachment' in the has_one association I get the error => uninitialized constant User::Avatar 如果我删除has_one关联中的:class_name =>'attachment',我会得到错误=> uninitialized constant User :: Avatar
I need to also attach avatars to blog posts, so I need the association to be polymorphic (or atleast i think so) 我还需要将头像附加到博客帖子,所以我需要关联是多态的(或至少我认为如此)
I am stumped and any help would be greatly appreciated. 我很难过,任何帮助都将不胜感激。
I do have a project in the works that is successfully using Paperclip and polymorphic associations. 我确实有一个项目正在成功使用Paperclip和多态关联。 Let me show you what I have, and maybe you can apply it to your project:
让我告诉你我有什么,也许你可以将它应用到你的项目中:
class Song < ActiveRecord::Base
...
has_one :artwork, :as => :artable, :dependent => :destroy
accepts_nested_attributes_for :artwork
...
end
class Album < ActiveRecord::Base
...
has_one :artwork, :as => :artable, :dependent => :destroy
accepts_nested_attributes_for :artwork
...
end
class Artwork < ActiveRecord::Base
belongs_to :artable, :polymorphic => true
attr_accessible :artwork_content_type, :artwork_file_name, :artwork_file_size, :artwork
# Paperclip
has_attached_file :artwork,
:styles => {
:small => "100",
:full => "400"
}
validates_attachment_content_type :artwork, :content_type => 'image/jpeg'
end
the songs form and the albums form include this as a partial: 歌曲形式和专辑形式包括这个部分:
<div class="field">
<%= f.fields_for :artwork do |artwork_fields| %>
<%= artwork_fields.label :artwork %><br />
<%= artwork_fields.file_field :artwork %>
<% end %>
don't forget to include :html => { :multipart => true } with the form 不要忘记在表单中包含:html => {:multipart => true}
artworks_controller.rb artworks_controller.rb
class ArtworksController < ApplicationController
def create
@artwork = Artwork.new(params[:artwork])
if @artwork.save
redirect_to @artwork.artable, notice: 'Artwork was successfully created.'
else
redirect_to @artwork.artable, notice: 'An error ocurred.'
end
end
end
and finally, an excerpt from songs_controller.rb: 最后,来自songs_controller.rb的摘录:
def new
@song = Song.new
@song.build_artwork
end
I'm not sure you really need to be polymorphic. 我不确定你真的需要多态。 How about this approach, which uses has_many :through?
这种方法怎么样,使用has_many:through? In plain English, the user has one avatar which has multiple images, and through this association you can call User.images to get the collection of images associated with the avatar.
在简单的英语中,用户具有一个具有多个图像的化身,并且通过该关联,您可以调用User.images以获得与化身相关联的图像的集合。
http://guides.rubyonrails.org/association_basics.html http://guides.rubyonrails.org/association_basics.html
class User < ActiveRecord::Base
has_one :avatar
has_many :images, :through => :avatar
end
class Avatar < ActiveRecord::Base
belongs_to :user
has_many :images
end
class Image < ActiveRecord::Base
belongs_to :avatar
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end
Having said all of this, I am left to wonder why you need to go through all this anyway. 说完所有这些之后,我不禁想知道为什么你还需要经历这一切。 Why not just do
为什么不这样做呢
class User < ActiveRecord::Base
has_many :avatars
end
which would give you as many images (avatars) as you want. 这将为您提供任意数量的图像(化身)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.