Rails 3与回形针和多个模型的多态关联 [英] 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.
附件模型:
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
用户模型:
has_one :avatar, :as => :attachable, :class_name => 'Attachment', :conditions => {:type => 'avatar'}
accepts_nested_attributes_for :avatar
用户控制器:
def edit
@user.build_avatar
end
用户查看表单:
<%= 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
如果我在has_one关联中删除:class_name =>'attachment',则会收到错误=> 未初始化的常量User :: Avatar
if I remove the :class_name => 'attachment' in the has_one association I get the error => 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.
推荐答案
我的作品中确实有一个成功使用Paperclip和多态关联的项目.让我向您展示我拥有的东西,也许您可以将其应用于您的项目:
I do have a project in the works that is successfully using Paperclip and polymorphic associations. 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 %>
不要忘记在表单中包含:html => {:multipart => true}
don't forget to include :html => { :multipart => true } with the form
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
最后,是songs_controller.rb的摘录:
and finally, an excerpt from songs_controller.rb:
def new
@song = Song.new
@song.build_artwork
end
这篇关于Rails 3与回形针和多个模型的多态关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!