Foo
Foo

Reputation: 810

Why my view with if statement won't work fine?

I get this output. the number shown in output is post.user.id.
Bubble should be switched back and forth if it was different id.
But it judges it's the same person even if it's not.
Why?

Current Output

2            Good, you?          > Person B
1            How have you been?  > person A
2            What's up?          > person B
Person A < Hello!!!!               1

I wasnt this Output

2            Good, you?          > Person B
Person A < How have you been?      1
2            What's up?          > person B
Person A < Hello!!!!               1

View

<% who = "" %>
<% @posts.each do |post| %>

    <tr id="<%= dom_post_id(post) %>">

        <% if post.user.id == who %>
            <td><%= post.user.nickname if post.user %></td>
            <td><div class="bubble me"><%= post.body %></div></td>
            <td><%= post.user.id %></td>
        <% else %>
            <td><%= post.user.id %></td>
            <td><div class="bubble you"><%= post.body %></div></td>
            <td><%= post.user.nickname if post.user %></td>     
        <% end %>
    </tr>
    <% who = post.user.id %>
<% end %>

Upvotes: 0

Views: 97

Answers (2)

MrYoshiji
MrYoshiji

Reputation: 54882

You should refactor your code like this:

<% user_id_for_left_side = @posts.first.try(:user_id) %>
<% @posts.each do |post| %>

  <tr id="<%= dom_post_id(post) %>">    
    <% if post.user_id == user_id_for_left_side %>
      <td><%= post.user.nickname %></td>
      <td><div class="bubble me"><%= post.body %></div></td>
      <td><%= post.user_id %></td>
    <% else %>
      <td><%= post.user_id %></td>
      <td><div class="bubble you"><%= post.body %></div></td>
      <td><%= post.user.nickname %></td>     
    <% end %>
  </tr>

<% end %>

Why using post.user_id instead of post.user.id ? Because doing post.user_id is using less resources than post.user.id.

Why deleting if post.user ? It is implicit that every Post belongs to a User (you should have a presence validation on Post model for the attribute user_id). This means a post always have a user associated with, no need to check for its presence.

Upvotes: 2

jonhopkins
jonhopkins

Reputation: 3842

<% who = -1 %>
<% @posts.each do |post| %>

    <% if who == -1 %>
        <% who = post.user.id %>
    <% end %>

    <tr id="<%= dom_post_id(post) %>">

        <% if post.user.id == who %>
            <td><%= post.user.nickname if post.user %></td>
            <td><div class="bubble me"><%= post.body %></div></td>
            <td><%= post.user.id %></td>
        <% else %>
            <td><%= post.user.id %></td>
            <td><div class="bubble you"><%= post.body %></div></td>
            <td><%= post.user.nickname if post.user %></td>     
        <% end %>
    </tr>

<% end %>

The added if block will set the variable who to the id of the user of the first post the loop encounters, and then not set it again. In every iteration of the loop after that, post.user.id will be who every time the post has that id, and the nickname will be on the left. For the posts with a different id, the nickname will be on the right.

Upvotes: 1

Related Questions