0

I am having a weird problem. I have friendship system in the site and I am trying to find out common friends. And to do that, I used intersection but it outputs in view, which doesn't make sense at all.

I have this in my helper.

  def display_connections_buttons(friend_id)
    if(@profile.id == current_user.id) || (@profile.username == current_user.username)

    else
      common_friends = @profile.friends & current_user.friends
      common_friends.each do |friend|
        if friend.id == friend_id
          render 'profiles/connections_buttons'
        end
      end
    end
  end

According to common friends, I am planning to render buttons. But I am getting this output on the screen:

#<User id: 9, first_name: "REMOVED", last_name: "REMOVED", email: "REMOVED", password_digest: "$2a$10$xkaCxStsc70BYpJN9yulQOuDyXOBJEIEaW817YDst9t...", authorization_token: "REMOVED", username: "", school: "REMOVED", program: nil, information: nil, website: nil, courses: nil, groups: nil, picture_id: nil, created_at: "2015-09-24 19:51:52", updated_at: "2015-09-24 19:51:52">]

I replaced some of the data with 'REMOVED'. I don't want it to appear.

I removed some of the code to understand where output comes from, and when I remove 'common_friends = @profile.friends & current_user.friends' output disappears from screen. Why do you think this piece of code outputs that?

Thank you.

View File

<span class="list-group-item title">Connections</span>

<div class="connections clearfix">
  <% @profile.friends.each do |friend| %>
      <div class="connection pull-left">
        <div class="content pull-left">
          <div class="connection-thumbnail pull-left">
            <%= image_tag friend.picture_id.blank? ? 'default.png' : friend.picture_id, :class => 'frame' %>
          </div>
          <div class="connection-name">
            <%= link_to friend.first_name + ' ' + friend.last_name, profile_path(friend.id) %>
          </div>
          <div class="connection-information">
            <div class="information"><%= friend.website %></div>
            <div class="information"><%= friend.colour %></div>
          </div>
        </div>
        <div class="connection-dropdown">
          <%= display_connections_buttons(friend.id) %>
        </div>
      </div>
  <% end %>
</div>

After I tried @Swards way, I am getting output of render as text. Not the variables.

<div class="dropdown pull-right"> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> Options <span class="caret"></span> </button> <ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> <li><a href="#">Send Message</a></li> <li role="separator" class="divider"></li> <li><a href="#">Disconnect</a></li> </ul> </div>
cyonder
  • 852
  • 1
  • 15
  • 36

1 Answers1

1

I think it's because this code...

  common_friends = @profile.friends & current_user.friends
  common_friends.each do |friend|
    if friend.id == friend_id
      render 'profiles/connections_buttons'
    end
  end

is the last to run in the method and is returned to the view and rendered as a string. The render isn't doing what you think it's doing, the result is being lost. Instead, set that to a variable and return it

  output = ""
  common_friends = @profile.friends & current_user.friends
  common_friends.each do |friend|
    if friend.id == friend_id
      output += render('profiles/connections_buttons')
    end
  end
  return output

A note - this will hopefully get you to see how this works, but you may want to refactor and do the render from the view. Put a method on the model to return common friends and render them in the view. In the end you won't use this helper at all.

Mark Swardstrom
  • 17,217
  • 6
  • 62
  • 70
  • I tried your way, I am getting output of render now. I edited the post. By the way, thank you for trying to fix. – cyonder Sep 27 '15 at 17:18
  • writing a method in a model is a good way to approach this problem but I cannot write. I am getting errors. If you would like to see my models, take a look at here: http://stackoverflow.com/questions/32809083/displaying-mutual-friends/32810542#32810542 – cyonder Sep 27 '15 at 17:31
  • it works! You are amazing. Additionally, I putted .html_safe at the end of return output, and now it renders buttons. Works perfect. But could you explain little bit in more detail why we saved that in a variable. – cyonder Sep 27 '15 at 17:45
  • 1
    The return value of the `.each` method is the enumerator (the array). The result of this would be similar to this in the view `= @profile.friends`. Instead, you need to return the html string you really want. – Mark Swardstrom Sep 27 '15 at 18:52