2014-08-28 48 views
0

我开始在rails控制台中使用pry。 当我得到一个Rails模型的实例,该值没有线所示打破这样的:如何使用Rails控制台中的换行符显示实例的值

pry(#<Class:0x1022f60e0>):1> first 
=> #<Article id: 1, name: "What is Music", content: "Music is an art form in which the medium is sound o...", created_at: "2011-08-24 20:35:29", updated_at: "2011-08-24 20:37:22", published_at: "2011-05-13 23:00:00"> 

http://railscasts.com/episodes/280-pry-with-rails?view=asciicast

有没有办法,以显示与换行符这样的价值观?

Article 
id: 1 
name: "What is Music" 
content: "Music is an art form in which the medium is sound o..." 
created_at: "2011-08-24 20:35:29" 
updated_at: "2011-08-24 20:37:22" 
published_at: "2011-05-13 23:00:00" 
+0

你试过'.to_yaml'? – PinnyM 2014-08-28 18:17:56

回答

0

我想,下面的会为你工作。

[email protected]:~/Rails/model_prac> rails c 
Loading development environment (Rails 4.1.4) 
2.1.2 :001 > Comment.first 
    Comment Load (0.4ms) SELECT "comments".* FROM "comments" ORDER BY "comments"."id" ASC LIMIT 1 
=> #<Comment id: 1, value_old: "I am a good Boy.", value_new: "I am a bad Boy.", created_at: "2014-08-02 17:36:14", updated_at: "2014-08-02 18:21:42"> 
2.1.2 :002 > y Comment.first 
    Comment Load (0.4ms) SELECT "comments".* FROM "comments" ORDER BY "comments"."id" ASC LIMIT 1 
--- !ruby/object:Comment 
attributes: 
    id: 1 
    value_old: I am a good Boy. 
    value_new: I am a bad Boy. 
    created_at: 2014-08-02 17:36:14.249466000 Z 
    updated_at: 2014-08-02 18:21:42.511522000 Z 
=> nil 
2.1.2 :003 > 
1

我建议您安装awesome_print

将它添加到您的Gemfile

group :development do 
    gem 'awesome_print' 
end 

而且随着bundle install安装。

现在使用ap打印它在控制台:

pry(#<Class:0x1022f60e0>):1> ap first 

#<Article:0x1022f60e0> { 
      :id => 1, 
     :name => "What is Music" 
     :content => "Music is an art form in which the medium is sound o..." 
    :created_at => "2011-08-24 20:35:29" 
    :updated_at => "2011-08-24 20:37:22" 
:published_at => "2011-05-13 23:00:00" 
} 
相关问题