【发布时间】:2017-02-10 05:14:47
【问题描述】:
我喜欢能够轻松地将数据结构打印到标准输出中,pretty-print 之类的工具非常适合:
[8] pry(main)> foo
=> {"utf8"=>"", "authenticity_token"=>"0G0OwiWMK3CRmEdOlmxgNA5VrIIm7/iHg21AOLaSywEPnDGHTtGLcLjSATY7BN5lzucIrdCwbv9M+Nw++2tuYg==", "q"=>"foobar", "commit"=>"Some submit button", "controller"=>"statics", "action"=>"create"}
[9] pry(main)> p foo
{"utf8"=>"", "authenticity_token"=>"0G0OwiWMK3CRmEdOlmxgNA5VrIIm7/iHg21AOLaSywEPnDGHTtGLcLjSATY7BN5lzucIrdCwbv9M+Nw++2tuYg==", "q"=>"foobar", "commit"=>"Some submit button", "controller"=>"statics", "action"=>"create"}
=> {"utf8"=>"", "authenticity_token"=>"0G0OwiWMK3CRmEdOlmxgNA5VrIIm7/iHg21AOLaSywEPnDGHTtGLcLjSATY7BN5lzucIrdCwbv9M+Nw++2tuYg==", "q"=>"foobar", "commit"=>"Some submit button", "controller"=>"statics", "action"=>"create"}
[10] pry(main)> pp foo
{"utf8"=>"",
"authenticity_token"=>
"0G0OwiWMK3CRmEdOlmxgNA5VrIIm7/iHg21AOLaSywEPnDGHTtGLcLjSATY7BN5lzucIrdCwbv9M+Nw++2tuYg==",
"q"=>"foobar",
"commit"=>"Some submit button",
"controller"=>"statics",
"action"=>"create"}
=> {"utf8"=>"", "authenticity_token"=>"0G0OwiWMK3CRmEdOlmxgNA5VrIIm7/iHg21AOLaSywEPnDGHTtGLcLjSATY7BN5lzucIrdCwbv9M+Nw++2tuYg==", "q"=>"foobar", "commit"=>"Some submit button", "controller"=>"statics", "action"=>"create"}
是否有一些工具可以让我将某些东西漂亮地打印到视图或闪存中?例如,如果我在Statics#create
def create
flash[:notice] = params.inspect
redirect_to statics_url
end
那么渲染出来的html是这样的:
有没有一种简单的方法可以漂亮地打印参数,以便它们显示在我的视图或闪存中?
我尝试过使用pretty_inspect:
application.html.erb:
<body>
<% if flash[:notice] %>
<%= render plain: flash[:notice] %>
<% end %>
<%= yield %>
</body>
statics_controller.rb:
def create
flash[:notice] = params.pretty_inspect
redirect_to statics_url
end
但它不起作用:
使用debug 也会呈现一个不漂亮的哈希:
<body>
<%= debug(params) if Rails.env.development? %>
<%= debug(flash[:notice]) if Rails.env.development? %>
<%#= debug(@collection) if Rails.env.development? %>
<%#= debug(current_user) if Rails.env.development? %>
<%= yield %>
</body>
编辑
有没有比gsubbing 更好的方法?:
<%= debug(flash[:notice].inspect.gsub(',', ",\n").gsub('\\','')) if Rails.env.development? %>
【问题讨论】:
标签: html ruby-on-rails pretty-print developer-tools flash-message