Paperclip custom path with id belongs_to - ruby-on-rails-3

I'm using paperclip inside a project.
The concerned model look like this :
class Asset < ActiveRecord::Base
has_paper_trail # Track model with paper_trail
has_attached_file :image, :styles => { :medium => "300x300>"},
:path => ":rails_root/public/attachments/project_#{:project_id}/ressources/:basename_:style.:extension"
belongs_to :project
end
As you see I want to have the id of concerned project in my image path, how can I make this ?

Try this
has_attached_file :image, :styles => { :medium => "300x300>"},
path => ":rails_root/public/attachments/:project_id/ressources/:basename_:style.:extension"
and then write
Paperclip.interpolates :project_id do |attachment, style|
"project_#{attachment.instance.project_id}"
end

Related

inconsistant photo url using paperclip when query in two apps

I am using paperclip to upload photos in my two rails app which share a single database. Now the problem is, if I upload a photo in app-a, for instance, paperclip gives me a url as:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-a/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Here is what I set up in my item model:
class Item < ActiveRecord::Base
attr_accessible :photo, :photo_url, :poll_id, :brand, :number_of_votes
has_attached_file :photo,
:styles => { :thumbnail => "100x100#",
:small => "150x150>",
:medium => "250x250>",
:large => "400x400>" },
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:url=>"/item_:id/created_at_:created_at/:style.jpg"
Paperclip.interpolates :created_at do |attachment, style|
attachment.instance.created_at
end
end
In the other app app-b, when I query the url with item.photo.url(:large), it gave me:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-b/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Therefore I got a wrong url.
Are there any ways I can do that by configuring paperclip? Thanks!
As described here : https://github.com/thoughtbot/paperclip#defaults , you can change the default config, in your case I guess you want to change the :local_root so it doesn't include the app name.

paperclip custom path and url for polymorphic associations

I am using attachment model as polymorphic association. How to change path and url parameters based on the associations.
Attachment Model
class Attachment < ActiveRecord::Base
belongs_to :user
belongs_to :attachable, :polymorphic => true
has_attached_file :attachment,
:url => "/attachments/:id/:basename.:extension",
:path => ":rails_root/public/attachments/:id/:basename.:extension",
:default_url => "/attachments/original/no-file.txt"
end
Project Model
class Project < ActiveRecord::Base
...
has_many :attachments, :as => :attachable, :dependent => :destroy
end
Client
class Client < ActiveRecord::Base
...
has_many :attachments, :as => :attachable, :dependent => :destroy
end
following path files are saving fine.
:path => ":rails_root/public/attachments/:id/:basename.:extension",
but I need to save files based on association like, for this how do pass the parameters to 'path'. 'attachable_type' defines the which association upload file belongs
/attachments/project/
/attachments/client/
You can use Paperclip Interpolations. Interpolations allows you to call a method to determine the value of a part of the path.
class Attachment < ActiveRecord::Base
belongs_to :user
belongs_to :attachable, :polymorphic => true
Paperclip.interpolates :attached_to do |attachment, style|
attachment.instance.attachable.class.to_s.downcase
end
has_attached_file :attachment,
:url => "/attachments/:id/:basename.:extension",
:path => ":rails_root/public/attachments/:attached_to/:id/:basename.:extension",
:default_url => "/attachments/original/no-file.txt"
end
has_attached_file :attachment, :path => ":rails_root/public/attachments/#{lambda { |a| a.instance.images_path? ? 'project' : 'client' }}/:id/:basename.:extension"
def images_path?
if your pretty condition
#return true
else
#return false
end
end

Configuring Rails 3 + Polymorphic Image model + Paperclip and Amazon S3, No errors, but nothing uploading

I suspect the problem lies in the way I am creating the polymorphic image attribute. I am using fields_for in the form.
In this case a user can create a post and add an image using paperclip, storing it with S3.
I am using a polymorphic image model "post_image":
class PostImage < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
#.merge(PAPERCLIP_OPS)
has_attached_file :image, :styles => { :medium => "200x200>", :thumb => "50x50>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:id/:filename",
:bucket => "zounds-dev"
validates_attachment_presence :image
validates_attachment_size :image, :less_than => 5.megabytes
end
Post Model:
class Post < ActiveRecord::Base
has_many :post_images, :as => :imageable, :dependent => :destroy
.
.
.
accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true
end
New Post Form:
=form_for( setup_post(#post,current_user), :html => { :multipart => true}) do |f|
%dl
=f.fields_for :post_images do |ff|
=ff.file_field :image
%dt.field=f.label :name
%dd.field=f.text_field :name
%dt.field=f.label :description
%dd.field=f.text_area :description
=f.fields_for :user do |u|
=render "user_fields", :f => u
=f.fields_for :assignments do |ff|
=ff.check_box :_destroy, {:checked => ff.object.persisted?}, '0','1'
=ff.label :_destroy, ff.object.group.name
=ff.hidden_field :group_id
.action=f.submit "Save Post"
The setup_post helper method used in the Post form_for: (the groups stuff isn't relevant here)
def setup_post(post, current_user)
groups = current_user.groups_as_owner + current_user.groups_as_member
(groups - post.groups).each do |group|
post.assignments.build(:group => group)
end
post.assignments.sort_by {|x| x.group.name }
post_image = post.post_images.build
post
end
Post controller:
def new
#user = User.find(params[:user_id])
# #post = #user.posts.build
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #post }
end
end
def create
#user = current_user
#post = #user.posts.build(params[:post])
.
.
.
end
I suspect the problem is that I am using fields_for for the post_image attribute, but I've looked all over and can't figure out what the proper way to implement a polymorphic nested image attribute is.
I also did the s3sh amazon s3 console thing, and although I couldn't upload an image because I couldn't figure out how to pass in the right image path to the open() function, I connected to S3. My s3.yml file is set up correctly as well.
Thanks yall,
Brian
The issue was with the reject_if in accepts_nested_attributes for the Post model
accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true
commenting it out fixed the issue.

display first result in included model, RoR3

The following code displays all featured events, and all associated images of these, inside my view.
I'd like to know how I can display just the first image of each featured event.
# event model
class Event < ActiveRecord::Base
attr_accessible :title, :start_date, :end_date, :content, :is_featured, :assets_attributes
has_many :assets, :order => 'asset_order ASC'
accepts_nested_attributes_for :assets, :allow_destroy => true
end
# asset model
class Asset < ActiveRecord::Base
belongs_to :event
belongs_to :static
has_attached_file :asset, :styles => { :large => "660x270#", :medium => "300x300#", :thumb => "100x100#" }
end
# event model
class Event < ActiveRecord::Base
attr_accessible :title, :start_date, :end_date, :content, :is_featured, :assets_attributes
has_many :assets, :order => 'asset_order ASC'
accepts_nested_attributes_for :assets, :allow_destroy => true
end
# static controller
def show
#events = Event.where(:is_featured => 1).includes(:assets)
#static = Static.where(:id => params[:id]).first
...
end
# static show view
- #events.each do |event|
- event.assets.each do |asset|
=image_tag asset.asset.url(:medium)
= event.title
= event.start_date.to_date
Are you looking for something like this?
# static show view
- #events.each_with_index do |event, index|
- event.assets.each do |asset|
=image_tag asset.asset.url(:medium) unless index > 0
= event.title
= event.start_date.to_date

Rails 3, Paperclip - Custom Interpolations

I've been having some troubles making custom Interpolation, gone through every example I could find on web, but no matter what I did, had no success.
At the moment I have this:
Model
has_attached_file :photo,
:path => ":rails_root/public/images/:img_name-:style.:extension",
:styles => {
:original => '100x100',
:thumb => '30x30'
}
initializers/paperclip.rb
Paperclip.interpolates :img_name do |attachment, style|
attachment.instance.img_name
end
img_name is field populated in form on upload with the image.
The error I get on upload is:
Invalid argument - (C:/Users/.../stream20110410-384-stl2lk20110230-213-1fm2bab, C:/.../photo_upload/public/images/:img_name-original.jpg)
Seems to work if it's directly in the model:
class Model < ActiveRecord::Base
Paperclip.interpolates :img_name do |attachment, style|
attachment.instance.img_name
end
has_attached_file :photo,
:path => ":rails_root/public/images/:img_name-:style.:extension",
:styles => {
:original => '100x100',
:thumb => '30x30'
}
end