Digitalism Digitalism - 1 year ago 74
Ruby Question

if statement in ruby loop editing properties

I have two database tables, user and product
When the user creates a product the is placed into the product table under product.user_id

I'm writing a loop to displays all products "belonging" to the currently signed in user, it looks like this so far.

<% @product.each do |p| %>
<% if product.user_id = %>
<td><%= %></td>
<td><%= product.user_id %></td>
<td><%= %></td>
<td><%= %></td>
<% end %>
<% end %>

If the currently signed in user is user 2 for example, instead of filtering all products that don't have a of 2 it just changes the product.user_id for every product to 2 so that the returned table looks like so.

2 2 1
2 2 2
2 2 3
2 2 4

Without the if statement it returns the full product listing with all the correct properties.

Answer Source

You should use == not =:

<% if product.user_id == %>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download