Jordan Davis
Jordan Davis

Reputation: 875

Best way to loop through array

My controller passes this array to the view:

@ipd = Ipdw.connection.select_all("select * from [My Preferences]")

This is my view code:

<h1>Hello, Rails</h1>
<body>

 <% @ipd.each do |ip| %>
 <p><%= ip %></p>
 <% end %>

</body>

This is displayed in the browser:

{"Pref ID"=>1, "Source ID"=>1, "Username"=>"RS2559", "Field Name"=>"CATEGORY", "String Value"=>"IP Placemat Deliverables", "Integer Value"=>nil, "Decimal Value"=>nil, "Created Dt Tm"=>2009-08-10 03:01:36 UTC, "Update Dt Tm"=>2009-12-14 16:04:01 UTC}

{"Pref ID"=>2, "Source ID"=>1, "Username"=>"RS2559", "Field Name"=>"TYPE", "String Value"=>nil, "Integer Value"=>nil, "Decimal Value"=>nil, "Created Dt Tm"=>2009-08-10 03:01:40 UTC, "Update Dt Tm"=>2009-12-14 16:04:01 UTC}

...

I want to show the "Username" and "Field Name" in a table. How can I loop through the array to show only these columns in a readable matter?

Upvotes: 0

Views: 59

Answers (3)

jvillian
jvillian

Reputation: 20253

How about something like:

<h1>Hello, Rails</h1>
<body>

 <% @ipd.each do |ip| %>
  <p>
    <% ["Username", "Field Name"].each do |k| %>
      <%= ip[k] %>
    <% end %>
  </p>
 <% end %>

</body>

That, naturally, will just put the values on your page. You could do other things like build a table or make bullet points or whatever.

That might look something like:

<h1>Hello, Rails</h1>
<body>

  <table>
    <tr>
      <% @field_names.each do |field_name| %>
        <th>
          <%= field_name %>
        </th>
      <% end %>
    </tr>
    <% @ipd.each do |ip| %>
      <tr>
        <% @field_names.each do |field_name| %>
          <td>
            <%= ip[field_name] %>
          </td>
        <% end %>
      </tr>
    <% end %>
  </table>

</body>

This assumes in your controller you did something like:

@field_names = ["Username", "Field Name"]

That way, you can decide in your controller which fields you want (and in what order) and not have to futz with your html.erb file.

If you wanted to pretty up those label names a little, in your controller you could do:

@label_mapping = {"Username"=>"User Name", "Field Name"=>"Field Name"}

And then:

<h1>Hello, Rails</h1>
<body>

  <table>
    <tr>
      <% @field_names.each do |field_name| %>
        <th>
          <%= label_mapping[field_name] %>
        </th>
      <% end %>
    </tr>
    <% @ipd.each do |ip| %>
      <tr>
        <% @field_names.each do |field_name| %>
          <td>
            <%= ip[field_name] %>
          </td>
        <% end %>
      </tr>
    <% end %>
  </table>

</body>

Upvotes: 3

user8836907
user8836907

Reputation:

There are maybe 2 ways:

First you have a model for this kind of data:

<% @ipd.each do |ip| %>
<p><%= ip.username %></p>
<% end %>

Secound: Your Dataset you get from your external Database looks like a hash. So try something like this:

<% @ipd.each do |ip| %>
<p><%= ip["username"] %></p>
<% end %>

I think kind 2 is what you need ;).

In a view i wouldn't use a shorter way of looping, be cause the code get less readable with something like this: abc.each{|letter| "<li>#{letter}</li>"}

Upvotes: 2

spickermann
spickermann

Reputation: 107142

It looks like your query is returning an array of hashes. Just use the keys to pick the values you are interested in:

<% @ipd.each do |ip| %>
  <p>
    <%= ip['Username'] %><br />
    <%= ip['Field Name'] %>
  </p>
<% end %>

Upvotes: 5

Related Questions