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!

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

Daniel Beasley
Daniel Beasley
4,824 Points

Friendship request button not showing on page...

For whatever reason all my links work except when I click "Add Friend" on my profile page it directs me to the correct path, however on this page my menu shows but no Ruby code shows...

So I believe Im getting redirected correctly but I feel something is wrong with my controller or Ruby code on new.html.erb... I have tried different things but cant finger this one out..

any Ideas?

Thanks, Daniel

user_friendships_controller.rb

def new
  if params[:friend_id]
    @friend = User.find(params[: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    

user_friendships/new.html.erb

<% if @friend %>
    <h1><%= @friend.Biz_name %></h1>
    <p>Do you really want to friend <%= @friend.Biz_name %>?</p>

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

3 Answers

Moiz Malik
Moiz Malik
11,905 Points

Not sure what this "@friend.biz_name" is. I'm assuming you modified the code a bit from the course?

Here's what my files look like.

user_friendships_controller.rb

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

def create
    if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
    @friend = User.where(profile_name: params[:user_friendship][:friend_id]).first
    @user_friendship = current_user.user_friendships.new(friend: @friend)
    @user_friendship.save
    flash[:success] = "You are now friends with #{@friend.full_name}"
    redirect_to profile_path(@friend)
    else
        flash[:error] = "Friend required"
        redirect_to root_path
    end
end
end

user_friendships/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.profile_name %>
        <%= submit_tag "Yes, Add Friend",  class: "btn btn-primary" %>
        <%= link_to "Cancel", profile_path(@friend), class: "btn" %>
    </div>
<% end %>
<% end %>
Daniel Beasley
Daniel Beasley
4,824 Points

Hey Moiz thanks for the reply! Yeah I renamed some things and have some differnt Paths but all in all everything is the same... For example my full_name = Biz_name... And my profile_path is user_path..

For whatever reason my /new.html.erb page renders my application file aka my menu however my ruby code will not show on the page. I feel like for some reason I need something different in this line of code <% if @friend %>. I'v tried different things but nothing seems to work.. Everything in my application works except this one page..

Also the same thing happens in Heroku.... Below are my heroku logs ... Normally I can figure out an error from the logs but can't get this one..

2013-03-29T16:59:32+00:00 heroku[router]: at=info method=GET      path=/user_friendships/new?           friend_id=2 host=limitless-coast-4809.herokuapp.com fwd="216.99.121.207" dyno=web.1     connect=8ms service=35ms status=200 bytes=5135
2013-03-29T17:03:06+00:00 app[web.1]: Started GET "/user_friendships" for 216.99.121.207 at 2013-03-29 17:03:06 +0000
2013-03-29T17:03:06+00:00 app[web.1]: Processing by UserFriendshipsController#index as HTML
2013-03-29T17:03:06+00:00 app[web.1]:   Rendered pins/_form.html.erb (4.3ms)
2013-03-29T17:03:06+00:00 app[web.1]:   Rendered statuses/_form.html.erb (2.4ms)
   2013-03-29T17:03:06+00:00 app[web.1]: Completed 200 OK in 65ms (Views: 18.5ms | ActiveRecord: 36.7ms)
2013-03-29T17:03:06+00:00 app[web.1]:   Rendered layouts/_footer.html.erb (0.1ms)
2013-03-29T17:03:06+00:00 app[web.1]:   Rendered layouts/_header.html.erb (1.3ms)
2013-03-29T17:03:06+00:00 app[web.1]:   Rendered user_friendships/index.html.erb within layouts/application (9.3ms)
2013-03-29T17:03:08+00:00 app[web.1]: Started GET "/statuses" for 216.99.121.207 at 2013-03-29 17:03:08 +0000
2013-03-29T17:03:08+00:00 app[web.1]: Processing by StatusesController#index as HTML
2013-03-29T17:03:08+00:00 app[web.1]:   Rendered statuses/index.html.erb within layouts/application (23.2ms)
2013-03-29T17:03:08+00:00 app[web.1]:   Rendered layouts/_footer.html.erb (0.0ms)
2013-03-29T17:03:08+00:00 app[web.1]:   Rendered layouts/_header.html.erb (1.1ms)
2013-03-29T17:03:08+00:00 app[web.1]:   Rendered pins/_form.html.erb (4.5ms)
2013-03-29T17:03:08+00:00 app[web.1]:   Rendered statuses/_form.html.erb (2.1ms)
2013-03-29T17:03:08+00:00 app[web.1]: Completed 200 OK in 33ms (Views: 27.2ms | ActiveRecord: 5.9ms)
2013-03-29T17:03:10+00:00 app[web.1]: Started GET "/2" for 216.99.121.207 at 2013-03-29 17:03:10 +0000
2013-03-29T17:03:10+00:00 app[web.1]: Processing by UsersController#show as HTML
2013-03-29T17:03:10+00:00 app[web.1]:   Parameters: {"id"=>"2"}
2013-03-29T17:03:10+00:00 app[web.1]:   Rendered users/show.html.erb within layouts/application (18.9ms)
2013-03-29T17:03:10+00:00 app[web.1]:   Rendered layouts/_header.html.erb (1.3ms)
2013-03-29T17:03:10+00:00 app[web.1]:   Rendered layouts/_footer.html.erb (0.1ms)
2013-03-29T17:03:10+00:00 app[web.1]:   Rendered pins/_form.html.erb (4.0ms)
2013-03-29T17:03:10+00:00 app[web.1]:   Rendered statuses/_form.html.erb (2.5ms)
2013-03-29T17:03:10+00:00 app[web.1]: Completed 200 OK in 33ms (Views: 25.9ms | ActiveRecord: 6.0ms)
2013-03-29T17:03:10+00:00 heroku[router]: at=info method=GET path=/2 host=limitless-coast-4809.herokuapp.com fwd="216.99.121.207" dyno=web.1 connect=1ms service=43ms status=304 bytes=0
2013-03-29T17:03:12+00:00 app[web.1]: Started GET "/user_friendships/new?friend_id=2" for 216.99.121.207 at 2013-03-29 17:03:12 +0000
2013-03-29T17:03:12+00:00 app[web.1]:   Parameters: {"friend_id"=>"2"}
2013-03-29T17:03:12+00:00 app[web.1]: Processing by UserFriendshipsController#new as HTML
2013-03-29T17:03:12+00:00 app[web.1]:   Rendered layouts/_header.html.erb (1.7ms)
2013-03-29T17:03:12+00:00 app[web.1]:   Rendered layouts/_footer.html.erb (0.1ms)
 2013-03-29T17:03:12+00:00 app[web.1]:   Rendered pins/_form.html.erb (5.8ms)
2013-03-29T17:03:12+00:00 app[web.1]:   Rendered statuses/_form.html.erb (2.9ms)
2013-03-29T17:03:12+00:00 app[web.1]:   Rendered user_friendships/new.html.erb within layouts/application (0.0ms)
2013-03-29T17:03:12+00:00 app[web.1]: Completed 200 OK in 17ms (Views: 13.4ms | ActiveRecord: 2.1ms)

Thanks for helping! Daniel

Daniel Beasley
Daniel Beasley
4,824 Points

I got it to work! I wish I could say how but not totally sure...