在Rails模型中获取当前日期

时间:2013-02-20 21:54:00

标签: ruby-on-rails ruby datetime

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

Inside Photo.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)>'

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

由于

2 个答案:

答案 0 :(得分:4)

尝试:

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

答案 1 :(得分:2)

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

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