来自 Paperclip 的 Rails send_file 多种样式,如何避免代码重复? [英] Rails send_file multiple styles from Paperclip, how can I avoid code repetition?

查看:40
本文介绍了来自 Paperclip 的 Rails send_file 多种样式,如何避免代码重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在关联模型中使用的默认代码,用于从 Paperclip 下载图像作为 save_to.

This is the default code I'm using in an associated model to download images from Paperclip as a 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

路线

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 种不同的 Paperclip 样式,并且每个样式都必须可以下载.为此,我这样做了(我将只展示 6 个代码块中的 2 个):


But my application has near 6 Paperclip different styles declared in my model and each must be downloadable. To do that I did this (I'll show just 2 of the 6 code blocks):

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 ...

路线

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) 在代码中的第一个和默认方法中有一些散列,例如:

Here come my questions:
1) Can I do it in a cleaner/better way?
2) Must I move the blocks from my controller to the model or a new controller?
3) In the first and default method in the code there are some hashes like:

:filename => @month.wallpaper_picture_file_name,  
:type => @month.wallpaper_picture_content_type  

但在另一种方法中,我意识到我不需要使用它们.那些哈希是必要的吗?
4) 我称他们为哈希者".这是正确的吗?还有其他更正吗?

But in the other method I realized I didn't need to use them. Are those hashes necessary?
4) I call them 'hashers'. Is it correct? Any other correction?

PD:如果生产中的 send_file 失败,请将其更改为 send_data 或
在 config/production.rb 中注释掉这一行

PD: if send_file fails in Production, change it to send_data or
comment out this line in config/production.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:


or, You can further DRY this with your routes modifications and controller as:

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

并从您的视图中生成路径,例如:

and generate path from your views something like:

 = link_to 'iphone4', download_path(month.wallpaper_id, month.id,'iphone4') 
 = link_to 'iphone5', download_path(month.wallpaper_id, month.id,'iphone5') 

在上述两种情况下,您都可以相应地将私有方法移至墙纸/月模型.

In both above cases you can move your private method to the wallpaper/month model accordingly.

3) 在代码中的第一个和默认方法中有一些散列喜欢:

3) In the first and default method in the code there are some hashes like:

:filename => @month.wallpaper_picture_file_name, :type =>@month.wallpaper_picture_content_type

:filename => @month.wallpaper_picture_file_name, :type => @month.wallpaper_picture_content_type

但在另一种方法中,我意识到我不需要使用它们.是那些哈希是必要的?

But in the other method I realized I didn't need to use them. Are those hashes necessary?

:filename 使您可以控制作为附件发送的文件的文件名,您可以修改它并给出与实际文件不同的文件名.默认情况下,它会返回带有实际文件名的附件.

:filename gives you control over what will be the filename of the file you are sending as attachment, you can modify it and give different file name than of actual file. By default it will return attachment with its actual file name.

:type 的工作方式类似,您可以将其强制为其他内容类型;但是它不会有太大影响,因为您已经将文件作为附件呈现.

:type again works similarly, you can force it to some other content type; however it won't affect much since you are rendering the file already as an attachment.

它们不是散列,它们是散列的键、值,或者在这里可以简单地视为属性..

They are not hashes, they are the key,values to the hashes or can be considered simply as attributes here..

这篇关于来自 Paperclip 的 Rails send_file 多种样式,如何避免代码重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆