0

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 :).

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.