0

How can I add or append a ruby helper that iterates in an active record response??

I have tried this (with .html or .append):

$( ".result" ).html('<%= @products.each do |product| %><label>product</label><% end %>'); 

But the problem is that it return all the list of products like this:

productproduct[#<Product id: 35, name: "", price: nil, category: "", quantity: nil, qty_unit: "", company_id: 108, created_at: "2014-11-13 17:22:33", updated_at: "2014-11-13 17:22:33", cost: nil, seller_bonification: nil>, #<Product id: 36, name: "Salmon", price: 3000, category: "Pescado", quantity: 1, qty_unit: "kg", company_id: 105, created_at: "2014-11-13 17:25:27", updated_at: "2014-11-13 17:25:27", cost: nil, seller_bonification: nil>]

So, I get only the string "product" from product and not the iterated object itself, I cant access to each product. And after that, I get all the list of products in a Active Records way. I've also tried this without the "=", like this:

$( ".result" ).html('<% @products.each do |product| %><label>product</label><% end %>'); 

And I only get:

productproduct

I dont know how to solve this issue. Maybe, a better practice is to use gon gem, or turbolinks, or a hash?? Does anyone knows??

3 Answers 3

1

Change your code to :

$( ".result" ).html('<% @products.each do |product| %><label><%= product.name %?</label><% end %>'); 

if you want to print something from product ( e.g. name in my code sample).

So remove = for @product.each tag;

add <%= %> to the product and call whatever you want.

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

Comments

1

When using each, don't use <%= but <%

<%= tries to put return value of each. Even each has return value in Ruby

Comments

0

In erb template for print something <%= %> is used and while looping or condition check you have to use <% #condition %>

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.