0

I'm learning Ruby and Rails, and the tutorials have been saying I should set everything up in the controller first for what my ERB file is going to need. I'm having some issues doing that.

I'm getting a product catalog from an API and I'm getting back an array. Here's the start of it:

[{"Name"=>"3x4 Vinyl Magnet", "Description"=>"Made of durable high-gloss vinyl. Measures 3x4 inches and has rounded corners. Waterproof and scratch resistant.", "UnitPrice"=>8.99, "ProductCode"=>"Gift65001"

There are more multiple products.

In my controller file I have:

@http_party_json = JSON.parse(@response.body)

@http_party_json.each do |i|
  i.each do |x|
    @just_products = x['Products']
  end
end

@just_products.each do |grab|
  @product_code = grab['ProductCode']; @product_code.delete! ';'
  @just_names = grab['Name']
  @just_prices = grab['UnitPrice']
  @just_descriptions = grab['Description']
end

My ERB file is:

<div class="large-12 columns">
  <div class="row">
<% @just_products.each do %>
    <div class="large-3 columns panel radius" style="height: 600px"><h2><%= @just_names %></h2><br><h3><%= @just_prices %></h3><br>
    <p><%= @just_descriptions %></p>
    <button id="<%= @product_code %>">Add to Cart</button>
    </div>
  <% end %>
</div>

It's displaying the last item in the array for each result it's bringing back. If I transfer the grab loop from the controller to the ERB file, it works fine, but I want to learn how to do it correctly.

1
  • In <% @just_products.each do %> you have no |product|, so the loop is looping but not handing a product to the "inside" of the loop. If you use <% @just_products.each do |product| %> it will pass each item in the array to the loop. You then access their methods with <%= product.name %> and <%= product.description %>. edit I just realized you are not loading them from the DB. Commented Jan 5, 2014 at 2:40

2 Answers 2

2

I should set everything up in the controller first for what my erb file is going to need

I see the trouble... it's a question of interpretation of what the book is trying to say. In your code, this block,

@just_products.each do |grab|

and all the assignments within it is actually not doing anything for you. After it's done spinning through @just_products.each, the final assignment it makes to @just_descriptions is merely the last value of 'Description' in @just_products, which is what you're seeing in your view.

The book merely says to prepare everything that the erb view is going to need. This does not translate to, "assign every possible variable explicitly in the controller". It is perfectly acceptable to pass @just_products to your view and spin through it (via .each) in there.

If you prefer not to do this, your alternate option would be to build arrays of codes, names, descriptions, etc. in the controller. Then spin through each of these in your view. In this case, this line:

@just_descriptions = grab['Description']

would look more like:

@just_descriptions << grab['Description']

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

Comments

2

There reason it's showing the last one is because you are looping through the array and overwriting the ivar every time. So when you get to the last item in the array you have overwritten each preceding one.

The main thing you want to avoid doing in your view is making SQL queries. Here, you've already loaded everything into an array and that's enough.

<div class="large-12 columns">
  <div class="row">
<% @just_products.each do |product| %>
    <div class="large-3 columns panel radius" style="height: 600px"><h2><%= product['Name'] %></h2><br><h3><%= product['UnitPrice'] %></h3><br>
    <p><%= product['Description'] %></p>
    <button id="<%= product['ProductCode'].delete!(';') %>">Add to Cart</button>
    </div>
  <% end %>
</div>

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.