这是我在关联模型中使用的默认代码,用于从Paperclip下载图像作为save_to。
months_controller
def download
@wallpaper = Wallpaper.find(params[:wallpaper_id])
@month = @wallpaper.months.find(params[:id])
send_file @month.wallpaper_picture.path,
:filename => @month.wallpaper_picture_file_name,
:type => @month.wallpaper_picture_content_type,
:disposition => 'attachment'
end
<<p> 路线/strong> resources :wallpapers do
resources :months
end
match 'wallpaper/:wallpaper_id/download/:id' => 'months#download', :as => :download
视图/月/指数
- @months.each do |month|
= link_to 'default', download_path(month.wallpaper_id, month.id)
但是我的应用程序在我的模型中声明了近6个不同风格的回形针,每个都必须可下载。为了做到这一点,我这样做了(我将只显示6个代码块中的2个):
months_controller
def download_iphone4
@wallpaper = Wallpaper.find(params[:wallpaper_id])
@month = @wallpaper.months.find(params[:id])
@month = 'public/system/wallpaper_pictures/' + @month.id.to_s + '/iphone4/' + @month.wallpaper_picture_file_name
send_file @month,
:disposition => 'attachment'
end
def download_iphone5
@wallpaper = Wallpaper.find(params[:wallpaper_id])
@month = @wallpaper.months.find(params[:id])
@month = 'public/system/wallpaper_pictures/' + @month.id.to_s + '/iphone5/' + @month.wallpaper_picture_file_name
send_file @month,
:disposition => 'attachment'
end
def download_ipad ...
def download_1440 ...
def download_1680 ...
def download_1920 ...
etc ...
<<p> 路线/strong> match 'wallpaper_pictures/:wallpaper_id/iphone4/:id' => 'months#download_iphone4', :as => :download_iphone4
match 'wallpaper_pictures/:wallpaper_id/iphone5/:id' => 'months#download_iphone5', :as => :download_iphone5
match 'wallpaper_pictures/:wallpaper_id/ipad4/:id' => 'months#download_ipad', :as => :download_ipad
match 'wallpaper_pictures/:wallpaper_id/1440/:id' => 'months#download_1440', :as => :download_1440
match 'wallpaper_pictures/:wallpaper_id/1680/:id' => 'months#download_1680', :as => :download_1680
match 'wallpaper_pictures/:wallpaper_id/1920/:id' => 'months#download_1920', :as => :download_1920
视图/月/指数
- @months.each do |month|
= link_to 'iphone4', download_iphone4_path(month.wallpaper_id, month.id)
= link_to 'iphone5', download_iphone5_path(month.wallpaper_id, month.id)
= link_to 'ipad', download_ipad_path(month.wallpaper_id, month.id)
= link_to '1440', download_1440_path(month.wallpaper_id, month.id)
= link_to '1680', download_1680_path(month.wallpaper_id, month.id)
= link_to '1920', download_1920_path(month.wallpaper_id, month.id)
我的问题来了:1)我可以用一种更干净/更好的方式来做吗?
2)我必须将块从我的控制器移动到模型或新控制器吗?
3)在代码中的第一个和默认方法中有一些散列,如:
:filename => @month.wallpaper_picture_file_name,
:type => @month.wallpaper_picture_content_type
但在另一种方法中,我意识到我不需要使用它们。这些哈希值有必要吗?
4)我称它们为"哈希器"。对吗?还有其他更正吗?
PD:如果send_file在生产中失败,将其更改为send_data或
在配置/生产中注释掉这一行。rb
config.action_dispatch.x_sendfile_header = "X-Sendfile"
send_file只发送一个空文件
将控制器代码重构为:
def download_iphone4
send_file (myfile params, :iphone4), :disposition => 'attachment'
end
def download_iphone5
send_file (myfile params, :iphone5), :disposition => 'attachment'
end
def download_ipad ...
def download_1440 ...
def download_1680 ...
def download_1920 ...
private
def myfile params, style
@wallpaper = Wallpaper.find(params[:wallpaper_id])
@month = @wallpaper.months.find(params[:id])
'public/system/wallpaper_pictures/' + @month.id.to_s + "/#{style}/" +@month.wallpaper_picture_file_name
end
或者,你可以通过你的路由修改和控制器来进一步DRY:
routes.rb
match 'wallpaper/:wallpaper_id/download/:id/:style' => 'months#download', :as => :download
控制器def download
send_file (myfile params), :disposition => 'attachment'
end
private
def myfile params
@wallpaper = Wallpaper.find(params[:wallpaper_id])
@month = @wallpaper.months.find(params[:id])
'public/system/wallpaper_pictures/' + @month.id.to_s + "/#{params[:style]}/" +@month.wallpaper_picture_file_name
end
并从视图中生成路径,例如:
= link_to 'iphone4', download_path(month.wallpaper_id, month.id,'iphone4')
= link_to 'iphone5', download_path(month.wallpaper_id, month.id,'iphone5')
在以上两种情况下,你都可以将你的private方法移到wallpaper/month模型中。
3)在代码中的第一个和默认方法中有一些哈希值如:
:filename => @month。Wallpaper_picture_file_name,:type =>@month。wallpaper_picture_content_type
但在另一种方法中,我意识到我不需要使用它们。是这些哈希值是必要的吗?
:filename让你控制你作为附件发送的文件的文件名,你可以修改它并给出与实际文件不同的文件名。默认情况下,它将返回带有实际文件名的附件。
:type的工作原理类似,你可以强制它为其他内容类型;但是,它不会影响太大,因为您已经将文件作为附件呈现。
它们不是散列,它们是键,散列的值,或者可以简单地视为属性。