Rails using a nested path
I have the following:
<%= project_attachment_path(attachment.project, attachment) %>
This outputs: /projects/70/attachments/25
<%= attachment.download_url %>
This outputs: /attachments/25/original.jpg?1291407855
What I want is: /projects/70/attachments/25?original.jpg?1291407855
I tried this: <%= project_attachment_path(attachment.project, attachment.download_url) %>
But that errors? is there a way to make the above work?
Thanks
UPDATING with Download_url method
class Attachment < ActiveRecord::Base
def download_url(style = nil, include_updated_timestamp = true)
url = Paperclip::Interpolations.interpolate('/:class/:id/:style.:extension', attachment, style || attachment.default_style)
include_updated_timestamp && attachment.updated_at ? [url, attachment.updated_at].compact.join(url.incl开发者_如何学运维ude?("?") ? "&" : "?") : url
end
ROUTES:
resources :projects do
# Download Attachment
get "attachments/:id/:style.:format" => "attachments#download", :as => :attachment
end
resources :attachments do
collection do
get 'download', :as => :download
end
end
Rake Routes:
project_attachment GET /projects/:project_id/attachments/:id/:style.:format {:action=>"download", :controller=>"attachments"}
Ok: when you're using this... url = Paperclip::Interpolations.interpolate('/:class/:id/:style.:extension', attachment, style || attachment.default_style)
The '/:class/:id/:style.:extension'
portion is what's creating your erroneous URL: /attachments/25/original.jpg?1291407855
I'm not sure exactly the best way to get the "projects/project_id" part into your url, but you need to do something like this:
class Attachment < ActiveRecord::Base
def download_url(style = nil, include_updated_timestamp = true)
project_id = self.project.id.to_s
attachment_url = Paperclip::Interpolations.interpolate('/:class/:id/:style.:extension', attachment, style || attachment.default_style)
include_updated_timestamp && attachment.updated_at ? [url, attachment.updated_at].compact.join(url.include?("?") ? "&" : "?") : url
url = 'projects/' + project_id + attachment_url
end
There may be a more elegant way to do this, but I think this should get you on the right track. Hope it helps!
You can check all the available paths via:
rake routes
精彩评论