Rails使用嵌套路径

时间:2010-12-03 21:06:15

标签: ruby-on-rails ruby-on-rails-3

我有以下内容:

<%= project_attachment_path(attachment.project,attachment)%>

输出:/projects/70/attachments/25

<%= attachment.download_url%>

输出:/attachments/25/original.jpg?1291407855

我想要的是:/projects/70/attachments/25?original.jpg?1291407855

我试过了:<%= project_attachment_path(attachment.project, attachment.download_url) %>

但那个错误?有没有办法完成上述工作?

由于

使用Download_url方法更新

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.include?("?") ? "&" : "?") : url
  end

途径:

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"}

2 个答案:

答案 0 :(得分:1)

好的:当你使用这个......     url = Paperclip :: Interpolations.interpolate('/:class /:id /:style。:extension',attachment,style || attachment.default_style)

'/:class/:id/:style.:extension'部分正在创建错误的网址:/attachments/25/original.jpg?1291407855

我不确定完全将“projects / project_id”部分放入您的网址的最佳方式,但您需要执行以下操作:

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

可能有更优雅的方式来做到这一点,但我认为这应该让你走上正轨。希望它有所帮助!

答案 1 :(得分:-1)

您可以通过以下方式检查所有可用路径:

rake routes