2013-02-20 88 views
4

我試着讓我的軌道模型,像這裏面的當前日期:獲取當前日期的Rails型號

內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)>' 

我在做什麼錯?我如何獲得當前日期到這個變量?

謝謝

回答

4

嘗試:

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

如果你想分配一個變量,你正在使用哈希語法。它應該是這樣的:

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

我試過了。事實上,這是我嘗試的第一件事。但它沒有工作,所以我切換到上面的書面版本 – banditKing 2013-02-20 22:46:05

+2

只需擺脫冒號。 '「#{today_date} /#{attachment.instance.image_file_name}」' – Andrei 2013-02-21 02:40:49