Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Name is not displaying in user_friendships/new.html page

The user name is not displaying in my users_friendships/new page. All that is showing in its place is:

<User:0x007fdcf4a73450>

Would you like to add #<User:0x007fdcf4a73450>?

Thanks in advance!

NEW.HTML

   <% if @friend %>
<h1><%= @friend %></h1>

<p> Would you like to add <%= @friend %>?</p>

<%= form_for @user_friendship, method: :post do |form| %>
    <div class="form form-actions">
        <%= form.hidden_field :friend_id, value: @friend %>
        <%= submit_tag "Yes, Add Friend", class: 'btn btn-danger' %>
        <%= link_to "Cancel", current_user, class: 'btn btn-default' %>
    </div>  
<% end %>

   <% end %>

MODEL/ USER_FRIENDSHIP.rb

   class UserFriendship < ActiveRecord::Base
   belongs_to :user
   belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'
   attr_accessible :user_id, :friend_id, :user, :friend
   end

USER.FRIENDSHIPS_CONTROLLER

  class UserFriendshipsController < ApplicationController
  before_filter :authenticate_user!, only: [:new]

  def new
   if params[:friend_id]
   @friend = User.find(params[:friend_id])
  @user_friendship = current_user.user_friendships.new(friend: @friend)
   else
  flash[:error] = "Friend required"
   end
  rescue ActiveRecord::RecordNotFound
  render file: 'public/404', status: :not_found
  end

1 Answer

Robert Ho
PLUS
Robert Ho
Courses Plus Student 11,383 Points

It is only showing <User:0x007fdcf4a73450> because that is what you are asking for by doing

<%= @friend %>

In order to get a specific attribute, you have to send a message/call the attribute by doing something like

<%= @friend.name %>

Does your friend model have a name attribute?

Thank you! I can't believe I completely overlooked that.