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

Creating the Friendship Form For Treebook Problems

Hello, im having problems setting up the Adding Friend mechanism on my treebook. At about at 11:55 in the "Creating the Friendship Form" video is where im stuck on. When i click on the add friend button, this is what i get,

"Routing Error

No route matches [GET] "/user_friendships/new"

Try running rake routes for more information on available routes. "

And these are my following codes for that part.

show.html.erb

<div class="page-header">
    <h1><%= @user.full_name%></h1>
    <%= link_to "Add Friend", new_user_friendship_path(friend_id: @user), class: 'btn' %>
</div>
<% if @statuses %>
    <% @statuses.each do |status| %>
    <div class="well">
        <%= status.content %>
        <hr />
        <%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
        </div>
    <% end %>
<% end %>

new.html.erb

<% if @friend %>
    <h1><%= @friend.full_name %></h1>
   <p>Do you really want to friend <%= @friend.full_name %>?</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: 'but btn-primary' %>
            <%= link_to "Cancel", profile_path(@friend), class: 'btn' %>
        </div>
    <% end %>
<% end %>
class UserFriendshipsController < ApplicationController
    before_filter :authenticate_user!, only: [:new]


def new
    if params[:friend_id]
            @friend = User.where(profile_name: params[:friend_id]).first
            raise ActiveRecord::RecordNotFound if @friend.nil?
            @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
end
routes.rb

Treebook::Application.routes.draw do
  get "profiles/show"
  devise_for :users

  devise_scope :user do
    get 'register', to: 'devise/registrations#new', as: :register
    get 'login', to: 'devise/sessions#new', as: :login
    get 'logout', to: 'devise/sessions#destroy', as: :logout
  end

  resources :user_friendships

  resources :statuses
  get 'feed', to: 'statuses#index', as: :feed
  root to: 'statuses#index'

  get '/:id', to: 'profiles#show', as: 'profile'
user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me,
                  :first_name, :last_name, :profile_name

  validates :first_name, presence: true

  validates :last_name, presence: true

  validates :profile_name, presence: true,
                           uniqueness: true,
                           format: {
                             with: /^[a-zA-Z0-9_-]+$/,
                             message: 'Must be formatted correctly.'
                           }

has_many :statuses
has_many :user_friendships
has_many :friends, through: :user_friendships

  def full_name
    first_name + " " + last_name
  end

  def to_param
    profile_name
  end

  def gravatar_url
    stripped_email = email.strip
    downcased_email = stripped_email.downcase
    hash = Digest::MD5.hexdigest(downcased_email)

    "http://gravatar.com/avatar/#{hash}"

  end
end

Any suggestions on what to do?

1 Answer

Hi Tristin, please shoot an email to help@teamtreehouse.com about this. They'll likely put you in touch with the teacher who can help you directly!