byCoder
byCoder

Reputation: 9184

Rails view hash in view from controller

I have such controller action:

@constr_num.each do |o|
      as_oem = get_from_as_oem(o.ARL_SEARCH_NUMBER)
      if as_oem.present?
        oem_art << as_oem
      end
    end    
    @oem_art = oem_art.to_a.uniq

get_from_as_oem looks like this:

def get_from_as_oem(oem)  

    require 'mechanize'
   *************************
    html = page.body

    doc = Nokogiri::HTML(html)
    doc.encoding = 'utf-8'

    rows = doc.search('//table[@id = "MainContent_GridView1"]//tr')
      @details = rows.collect do |row|
          detail = {}
          [
            [:car, 'td[1]/text()'],
            [:article, 'td[2]/text()'],
            [:group, 'td[3]/text()'],
            [:price, 'td[4]/text()'],
          ].each do |name, xpath|
            detail[name] = row.at_xpath(xpath).to_s.strip
          end
          detail
      end
    @details  
  end

if in view i write: =@oem_art i get

[[{:car=>"", :article=>"", :group=>"", :price=>""}, {:car=>"Volkswagen", :article=>"1C0959799B 001", :group=>"STEUERG.", :price=>"274,22"}, {:car=>"Volkswagen", :article=>"1C0959799B 003", :group=>"STEUERG.", :price=>"274,22"}, {:car=>"Volkswagen", :article=>"1C0959799B 00E", :group=>"STEUERG.", :price=>"274,22"}, {:car=>"Volkswagen", :article=>"1C0959799B 00F", :group=>"STEUERG.", :price=>"274,22"}, {:car=>"Volkswagen", 
etc...

so how could i view it normal, like .each |c| c.car etc...

Upvotes: 0

Views: 481

Answers (2)

Marlin Pierce
Marlin Pierce

Reputation: 10081

<% @oem_art.each_pair do |oem_key, oem_value| %>
  <%= oem_key %> => <%= oem_value %></br>
<% end %>

Upvotes: 1

2called-chaos
2called-chaos

Reputation: 3078

You can either use the terminal or the browser for that. You can use puts myhash.inspect to print your "dumped" hash to the terminal resp. logfile.

Or you do something like this in your controller: render text: myhash.inspect

If you prefer solution one you can also enhance this experience with ap which is the gem awesome_print which provides prettier and colorized output in your console. On top of that you can give pry a try. With that you can set breakpoints and then start an interactive console right in that place.

Awesome print: https://github.com/michaeldv/awesome_print
Pry: https://github.com/pry/pry

Also: http://guides.rubyonrails.org/debugging_rails_applications.html

Upvotes: 0

Related Questions