Created
January 4, 2012 18:55
-
-
Save jathayde/1561451 to your computer and use it in GitHub Desktop.
Trying to figure out polymorphic association with image to other items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def new | |
@image = Image.new | |
respond_with @image | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Image < ActiveRecord::Base | |
belongs_to :parent, :polymorphic => true | |
has_attached_file :content, | |
:styles => { | |
:full => "800x800>", | |
:medium => "400x400>", | |
:list => "200x200>", | |
:thumb => "100x100#" | |
} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Piece < ActiveRecord::Base | |
belongs_to :project | |
has_many :images, :as => :parent | |
validates_presence_of :name | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p class="button-right"> | |
<%= link_to "Edit Piece", | |
edit_piece_path(@piece), | |
:class => "button icon edit" %> | |
</p> | |
<p> | |
<%= @piece.project.client.name %> » | |
<%= @piece.project.name %> » | |
</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