我正在尝试显示自创建帐户以来的时间(以分钟为单位)

I am trying to display the amount of time in minutes since an account was created

我正在 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 >

然后我用同样的class在这里显示实时时差:

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

但是,错误不断出现

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

谢谢大家的帮助!

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

我会将方法移动到模型中,像这样

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

然后在视图中

<%= current_user.minutes_since_created %>