4

我试图在我的 rails 模型中获取当前日期,如下所示:

内部照片.rb

  Paperclip.interpolates :prefix  do |attachment, style|
      :today_date => Date.today.to_s
      "#{:today_date}/#{attachment.instance.image_file_name}"
  end 

当我从客户端向服务器发送照片时,我收到一个错误,并在服务器上显示以下控制台输出。这告诉我“日期”功能有问题

服务器控制台:

 Started POST "/photos.json" for 127.0.0.1 at 2013-02-20 13:47:35 -0800
 13:47:35 web.1  | 
 13:47:35 web.1  | SyntaxError 
             (/Users/AM/Documents/RailsWS/test/app/models/photo.rb:22: syntax 
               error, unexpected tASSOC, expecting keyword_end
 13:47:35 web.1  |      :today_date => Date.today.to_s
 13:47:35 web.1  |                    ^):
 13:47:35 web.1  |   app/controllers/photos_controller.rb:1:in `<top (required)>'

我究竟做错了什么?如何将当前日期输入此变量?

谢谢

4

2 回答 2

5

尝试:

Paperclip.interpolates :prefix  do |attachment, style|
  "#{Date.today.to_s}/#{attachment.instance.image_file_name}"
end 
于 2013-02-21T05:26:47.540 回答
2

如果您尝试分配变量,则您使用的是哈希语法。它应该如下所示:

  Paperclip.interpolates :prefix  do |attachment, style|
      today_date = Date.today.to_s
      "#{today_date}/#{attachment.instance.image_file_name}"
  end 
于 2013-02-20T21:57:40.300 回答