Question

Currently I am working on simple Ruby on Rails web application, were I need to output some data to user. Data to output is all attributes of selected object. Currently I am achieving it by using a simple loop shown below

<%= @event.attributes.each do |attr_name, attr_value| %>
   <%= attr_name.humanize.titleize %>
   <%= attr_value %>
<% end %>

Everything works like a charm, except I get data (I believe hashes), which I do not want to see in curly brackets like:

{"id"=>30, "title"=>"Kim and Jim Wedding", "created_at"=>Sun, 27 Apr 2014 16:41:34 UTC +00:00, "updated_at"=>Sun, 27 Apr 2014 16:41:34 UTC +00:00, "venue"=>"Skinners hall", "post_code"=>"n78db", "description"=>"Wedding reception for 160", "staff_need"=>12, "price"=>5600.0, "profit"=>700.0}

Is there any chance I can hide this block? Or I should just loop every attribute in object manually like @event.id, @event.title, etc. instead of using attributes.each? Thank you.

Was it helpful?

Solution

Change

<%= @event.attributes.each do |attr_name, attr_value| %>

To

<% @event.attributes.each do |attr_name, attr_value| %>  ## Removed =

You are seeing a hash displayed in the view because you are rendering @event.attributes which I suppose you don't need to.

<% %> is for evaluation of Ruby expression and <%= %> for evaluation plus rendering the result of Ruby expression. In your case, you simply need <% %>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top