【问题标题】:Easy way to pretty print into view or flash?将漂亮打印到视图或闪存中的简单方法?
【发布时间】: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


    【解决方案1】:

    您可以使用JSON.pretty_generate

    <body>
      <pre>
        <%= preserve do %>
            <%= JSON.pretty_generate(params) if Rails.env.development? %>
            <%= JSON.pretty_generate(flash[:notice]) if Rails.env.development?%>
        <% end %>
      </pre>
      <%= yield %>
    </body>
    

    【讨论】:

    • undefined method preserve for #&lt;#&lt;Class:0x007fd3af29db98&gt;:0x007fd3af29c978&gt;
    • only generation of JSON objects or arrays allowed #to_a 帮助
    【解决方案2】:

    您可以在 application.html.erb 文件中添加类似 &lt;%= debug(params) if Rails.env.development? %&gt; 的内容

    例如,如果您想在视图中查看参数中的内容和控制器中的特定变量,您可以定义需要多少此类语句:

    <%= debug(params) if Rails.env.development? %>
    <%= debug(@collection) if Rails.env.development? %>
    <%= debug(current_user) if Rails.env.development? %>
    

    对于漂亮的输出,您应该使用引导程序 - 请检查 this link

    【讨论】:

    • 请看编辑,是否可以漂亮地打印参数或闪存?
    • 我刚刚编辑了我的帖子,您应该将引导程序包含到您的应用程序中,然后为调试(参数)输出定义一些 CSS。所有这些都在我在编辑时提供的链接中进行了描述。
    猜你喜欢
    • 2016-10-13
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 2012-02-09
    • 2013-06-21
    • 2021-02-10
    • 1970-01-01
    相关资源
    最近更新 更多