0

i'm trying to use a conditional inside a iteration but did not worked so, here the scenario:

in this case if the if the order or the product is present should just show the order and products with the feedback.

but even if is present show the feedback with odata and pdata.

someone know why?

<% @feedbacks.each do |feedback| %>
      <% if order.present?  && product.present?  %>
      <% order = feedback.order %>
      <% product = order.product %>
      <% else %>
<% odata = feedback.odata %>
  <% pdata = odata.pdata %>
  <% end %>
bookaka
  • 21
  • 6

1 Answers1

1

I guess this is what you are trying to do,

<% @feedbacks.each do |feedback| %>
  <% if (order = feedback.order).present? && (product = feedback.product).present? %>
    <%= order.title %>
    <%= product.title %>
  <% else %>
    <%= (odata = feedback.odata).name %>
    <%= odata.pdata.name %>
  <% end %>
<% end %>

Note: title and name are assumed columns, replace it with your required/respected attribute. Please go through this to understand the difference between various erb tags.

Comparison:

  • for the if condition you are trying to call order and product directly, which will throw error as they are related to feedback.
  • <% %> just executes the ruby code, you wanted to print the data so need to use <%= %>.
  • no need to save them in variable when you are not going to use it. I have saved them while checking the existence of the object in the condition and could use to display without querying the db.
Community
  • 1
  • 1
Md. Farhan Memon
  • 5,727
  • 2
  • 8
  • 35