Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bruce/1562113 to your computer and use it in GitHub Desktop.
Save bruce/1562113 to your computer and use it in GitHub Desktop.
Trying to figure out polymorphic association with image to other items
def new
@image = Image.new
respond_with @image
end
class Image < ActiveRecord::Base
belongs_to :parent, :polymorphic => true
has_attached_file :content,
:styles => {
:full => "800x800>",
:medium => "400x400>",
:list => "200x200>",
:thumb => "100x100#"
}
end
class Piece < ActiveRecord::Base
belongs_to :project
has_many :images, :as => :parent
validates_presence_of :name
end
<%= form_for @image, :html => { :multipart => true } do |f| %>
<%= f.hidden_field :parent_id %>
<%= f.hidden_field :parent_type %>
<%= f.file_field :content %>
<%= f.submit %>
<% end %>
<p class="button-right">
<%= link_to "Edit Piece",
edit_piece_path(@piece),
:class => "button icon edit" %>
</p>
<p>
<%= @piece.project.client.name %> &raquo;
<%= @piece.project.name %> &raquo;
</p>
<h1><%= @piece.name %></h1>
<p><%= @piece.description %></p>
<% if @piece.images.empty? %>
<p>
This piece has no images in the system. Why don't you
<%= link_to "add one?", new_client_project_piece_image_path(:piece_id => @piece.id, :project_id => @piece.project.id, :client_id => @piece.project.client.id) %>
</p>
<% else %>
<h2>Images in <%= @piece.name %></h2>
<ul class="images">
<%= render :partial => "images/image",
:collection => @piece.images,
:as => "image" %>
</ul>
<p>
<%= link_to "Add another image", new_client_project_piece_image_path(:parent_id => @piece.id, :project_id => @piece.project.id, :client_id => @piece.project.client.id) %>
</p>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment