awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.07k stars 454 forks source link

No HTML formatting Rails 4.x Windows 7. #291

Closed cmendla closed 7 years ago

cmendla commented 7 years ago

I installed awesome print but it doesn't seem to be formatting.

Gemfile

gem 'awesome_print', require: 'ap'

in my html.erb

<% require "awesome_print" %>
<%= ap @observation.inspect.html_safe %>
<hr>
<%=  @observation.inspect %>
<hr>
<%= ap Observation %>

What I see (removed middle part for readability)

<pre class="debug_dump">&quot;#&lt;Observation id: 344, employee_id: nil, supervisor_last_name: \&quot;Smith\&quot;, supervisor_first_name: \&quot;Greg\&quot;, department_id: 2, team_id: nil, . . . updated_at: \&quot;2016-06-27 17:15:39\&quot;, date_discussed: \&quot;2016-06-13\&quot;, reason: nil, points: nil, point_value_id: nil, supervisor_id: 117, employee_raw: \&quot;[\\\&quot;Chris Jones\\\&quot;]\&quot;, supervisor_raw: nil, user_id: 87, testchk: nil&gt;&quot;</pre>

`#<Observation id: 344, employee_id: nil, supervisor_last_name: "Smith", supervisor_first_name: "Bob", . . .  ., supervisor_id: 117, employee_raw: "[\"Chris Jones\"]", supervisor_raw: nil, user_id: 87, testchk: nil>`
. . . 
<pre class="debug_dump">class Observation < ActiveRecord::Base { :id<kbd style="color:slategray"> =&gt; </kbd><kbd style="color:darkcyan">:integer</kbd>, :employee_id<kbd style="color:slategray"> /kbd><kbd style="color:darkcyan">:integer</kbd>, :testchk<kbd style="color:slategray"> =&gt; </kbd><kbd style="color:darkcyan">:boolean</kbd> }</pre>

If I try using ap from the console, I get

> require "ap"
=> true
>> ap @observation
nil
=> nil
>> require "awesome_print"
=> false
>> ap @users
nil
=> nil

I did run bundle install and it seems to be installed


Using auto-session-timeout 0.9.2
Using awesome_print 1.7.0
Using debug_inspector 0.0.2

My environment is

Other things I use with html_safe such as ckeditor do show up in an html format.

gerrywastaken commented 7 years ago

Hi @cmendla-cct

I don't think it's anything to do with the environment. ap takes an object and pretty prints it. In your case you are passing it a string so there is not much for it to do other than putting some quotes around it.

When you call @observation.inspect you transform your observation variable into a string. You don't need inspect when using awesome print as that's essentially what we are doing: http://ruby-doc.org/core-2.3.3/Object.html#method-i-inspect

What you probably want to do is ap(@observation), or in your case because you want to then convert it to be a html safe string you will instead want to use the ai (stands for Awesome Inspect) method. So instead of: <%= ap @observation.inspect.html_safe %> you can write: <%= @observation.ai.html_safe %>

I'm guessing in your console example the instance variables were not available in that context and so they defaulted to nil (which I agree is unintuitive, but this is just what Ruby does).

With your:

>> require "awesome_print"
=> false

again this is just another strange Ruby thing that catches out a lot of people. false just means it's already been loaded. https://ruby-doc.org/core-2.3.3/Kernel.html#method-i-require If it can't require at all, it actually throws a https://ruby-doc.org/core-2.3.3/LoadError.html.

Hope that helps :)

cmendla commented 7 years ago

It is working well now.

What you probably want to do is ap(@observation),

was the key.

Thanks