在控制台使用Devise时出现“undefined method 'to_i'”错误

4

我第一次尝试使用Devise。我创建了一个基本的登录/注销系统,并尝试从控制台创建一些对象,注意到了一些奇怪的事情:

rails c --sandbox
Loading development environment in sandbox (Rails 4.1.5)
Any modifications you make will be rolled back on exit

Frame number: 0/20
[1] »  u = User.last
  User Load (0.7ms)  SELECT  "users".* FROM "users"   ORDER BY "users"."id" DESC LIMIT 1
(pry) output error: #<NoMethodError: undefined method `to_i' for #<Object:0x007fd988871540>>
[2] »  u
(pry) output error: #<NoMethodError: undefined method `to_i' for #<Object:0x007fd988871540>>
[3] »  u.id
=> 5

为什么控制台会在我的对象上抛出错误?User.last等情况也是一样。

这是我的模式:

create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
2个回答

4

简单的解决方法是将以下内容添加到.pryrc文件中

module AwesomePrint
  class Formatter

    private

    def awesome_self(object, type)
      if @options[:raw] && object.instance_variables.any?
        awesome_object(object)
      elsif object.respond_to?(:to_hash)
        awesome_hash(object.to_hash)
      else
        colorize(object.inspect.to_s, type)
      end
    end
  end
end

今天这个让我省了不少麻烦,如果还没有的话,这个需要被推荐并提交为Awesome Print的一个补丁。 - baweaver

2
你是否安装了gem jazzy_hands?在我们的情况下,这是原因。

是的,我有爵士手!那么你做了什么? - The Whiz of Oz
我们刚刚移除了jazz_hand,但是朋友告诉我问题直接来自于awesome_print在jazz_hand中。检查两个gems的问题列表,也许有解决方案,如果你不想移除jazz_hands。 - Adam Piotrowski
我从来没有想过 awesome_print 会引起这个问题。我从 gemfile 中删除了它,问题解决了。 - daniel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接