2015-03-31 25 views
0

我正在使用devise進行rails驗證。我在rails控制檯中運行了類似的東西,並顯示了正確的編號。我想顯示自創建帳戶以來的分鐘數

以下是我在控制檯運行:

Coles-MacBook-Pro-2:rq coleschiffer$ rails c 
Loading development environment (Rails 4.1.0) 
2.1.1 :001 > (Time.now - User.first.created_at.round)/60 
    User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 
=> 1004.61901355 
2.1.1 :002 > 

然後我用同一個類來顯示實時時間差在這裏:

<% Time.now - (user_created_at)/60.round %> 

但是,錯誤不斷彈出

undefined local variable or method `user_created_at' for #<#<Class:0x007fa66bfd0660>:0x007fa67186cc60>. 

謝謝大家的幫助!

+0

哪裏你是否定義了'user_created_at'? – 2015-03-31 19:03:29

+0

我認爲它是由設計定義的。 – user3312633 2015-03-31 19:11:05

+0

很明顯,它運行在錯誤的上下文中,或未定義,我會將該方法移至用戶模型並將其應用於current_user,是否適用於您? – 2015-03-31 19:12:24

回答

0

我在視圖中的方法轉移到模型中,這樣

class User < ActiveRecord::Base 
    def minutes_since_created 
    ((Time.now - created_at)/60).round 
    end 
end 

然後

<%= current_user.minutes_since_created %> 
0
<% Time.now - (@current_user.created_at)/60.round %> 

你爲什麼不嘗試time ago in words

<%= time_ago_in_words(@current_user.created_at) %> 
# if @user is where you keep your user 
# will produce '3 minutes' for example 
相關問題