Simon Kiely
Simon Kiely

Reputation: 6040

Formatting Ruby on Rails output

Let me prefix this by saying I am not a Rails/Ruby developer - I am just doing a small amount of work using an internal tool which generates Rails code. I am trying my best with it though!

So I am trying to iterate over an object and print all of its fields.

Currently I have this implemented like so :

  <% @subject.Association.each do |horse| %>
 <tr>
  <td>
    <%= horse['Name'].to_s %>
    <%= horse['Size'] %>
  </td>
  </tr>
   <% end %>

This works, except it will print

Output > ["Horse1"]["2 hands"]

instead of

Output > Horse 1 2 hands.

What can I do to remove the superfluous brackets and quotation marks?

Apologies if I have left out any pertinent details - as I say, still new to Ruby and learning :).

Upvotes: 0

Views: 110

Answers (2)

zkwsk
zkwsk

Reputation: 2116

The brackets and quotation marks suggest that what is in fact returned is two arrays, each with one string element inside.

It is difficult to say without more context about what you models look like, the way you retrieve horses seems a bit weird. If I was to iterate over a collection of horses, I would probably have a class variable in the controller @horses (note class variables are prefixed with '@' in order to make them available to your views).

If everything is set up right I should then be able to do something like

<% @horses.each do |horse| %>
  <%= horse.name %>
  <%= horse.size %>
<% end %>

(left out the markup for brevity).

Upvotes: 0

rneves
rneves

Reputation: 2083

try to use

horse['Name'].first

It's because your variables are arrays..

If you are using a SQL db try to use

horse.Name

and check if this works like you want.

Upvotes: 2

Related Questions