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 trialDena G
441 PointsSyntax Error in UserFriendshipsController#index
Hi all,
I am getting a SyntaxError in UserFriendshipsController#index for:
@user_friendship = current_user.user_friendships.all
I am not sure what I missed. Any assistance will be greatly appreciated! I'm still new to rails. Thanks in advance!
Controller
class UserFriendshipsController < ApplicationController
before_filter :authenticate_user!
def index
@user_friendship = current_user.user_friendships.all
end
Index
<% @user_Friendships.each do |friendship| %>
<% friend = friendship.friend %>
<div id="<%= dom_id(friendship) %>" class="friend row">
<div class="span1">
<center><%= link_to image_tag(user.avatar.url(:thumb)), user %></center>
</div>
<div class="span7">
<strong><%= friend.name %></strong><br />
<%if friendship.pending? %>
<em>Frienship is pending.</em> <%=link_to "Delete request", edit_user_friendship_path(friendship) %>.
<% end %>
<% if friendship.requested? %>
<em>Friendship requested.</em> <%= link_to "Accept Friendship", edit_user_friendship_path(friendship) %>.
<% end %>
<% if friendship.accepted? %>
<em>Friendship started <%= friendship.updated_at %>.</em> <%= link_to "Update friendship", edit_user_friendship_path(friendship) %>.
<% end %>
</div>
</div>
<% end %>
Model
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 :state
state_machine :state, initial: :pending do
after_transition on: :accept, do: :send_acceptance_email
state :requested
event :accept do
transition any => :accepted
end
end
def self.request(user1, user2)
transaction do
friendship1 = create!(user: user1, friend: user2, state: 'pending')
friendship2 = create!(user: user2, friend: user1, state: 'requested')
friendship1.send_request_email
end
4 Answers
Robert Ho
Courses Plus Student 11,383 PointsHey Rochelle,
It looks like in your controller, you have:
def index
@user_friendship = current_user.user_friendships.all
end
but in your view, you have an extra S in @user_friendshipS:
<% @user_Friendships.each do |friendship| %>
change it to this (without the S and the end):
<% @user_friendship.each do |friendship| %>
Good luck!
Robert Ho
Courses Plus Student 11,383 PointsIs this form on the same page? If not, which page is it on? And can you copy and paste your entire friendships controller for me?
Dena G
441 PointsWill do! Thank you for taking a look.
CONTROLLER
class UserFriendshipsController < ApplicationController
before_filter :authenticate_user!
def index
@user_friendship = current_user.user_friendships.all
end
def accept
@user_friendship = current_user.user_friendships.find(params [:id])
if @user_friendship.accept!
flash[:success] = "You are now friends with # {@user_friendship.friend.name}"
else
flash[:error] = "That friendship could not be accepted"
redirect_to user_friendships_path
end
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
def create
if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
@friend = User.find(params[:user_friendship][:friend_id])
@user_friendship = current_user.user_friendships.new(friend: @friend)
if @user_friendship.save
flash[:success] = "You are now friends!"
else
flash[:error] = "There was a problem."
end
redirect_to user_path(@friend)
else
flash[:error] = "Friend required"
redirect_to root_path
end
end
private
def user_friendships_params
params.require(:user_friendships).permit(:user_id, :friend_id, :user, :friend, :state, :user_friendship)
end
def edit
end
end
end
NEW
<% 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-primary' %>
<%= link_to "Cancel", current_user, class: 'btn btn-default' %>
</div>
<% end %>
<% end %>
Robert Ho
Courses Plus Student 11,383 PointsHey Rochelle,
Above you stated:
"Would you happen to know why when I click on my Add Friend button it displays a blank page instead of my form below? "
But I don't see any button called "Add Friend" anywhere(?). Do you mean the "Follow" button that you have?
- When you click the button, the page it links you to is COMPLETELY blank with nothing? I noticed that you put only the @friend instance variable instead of @friend.attribute. Lets say you wanted your friend's name...it should be done something like this:
<h1><%= @friend.full_name %></h1>
and
<p> Would you like to add <%= @friend.full_name %>?</p>
- If it is completely blank, do me a favor and type some random string like "hello!" before the <% if @friend %> ... < % end %> erb blocks and tell me if the "hello!" string shows up.
4, When you go to a profile URL, what is the URL? Is it something like localhost:3000/users/1? Or is it localhost:3000/users/rochelleprofile? or is it localhost:3000/rochelleprofile??
Dena G
441 PointsHi Robert,
Yes, I meant the "Follow" button, sorry and yes when I click on the "Follow" button it takes me to a complete blank page with my nav bar. The URL it directs me to for the user I am trying to add is: 0.0.0.0:3000/user_friendships/new?friend_id=2
I put hello in front of <% if @friend %> and it showed up! What does that mean? I am a noob to rails. It was working as shown above yesterday before I started working on the index page. I'm not sure where it went wrong.
Robert Ho
Courses Plus Student 11,383 PointsDo me a favor and put this line
raise ActiveRecord::RecordNotFound if @friend.nil?
before this line:
@user_friendship = current_user.user_friendships.new(friend: @friend)
So it will look like this in the end:
if params[:friend_id]
@friend = User.find(params[:friend_id])
raise ActiveRecord::RecordNotFound if @friend.nil?
@user_friendship = current_user.user_friendships.new(friend: @friend)
else
flash[:error] = "Friend required"
end
Then try it again. If you get an error called ActiveRecord::RecordNotFound, then that means something is wrong with that user object. Try creating another user and adding them.
Let me know your results.
Dena G
441 PointsUnfortunately, I still get a blank screen. Adding the line did not produce an error. This is puzzling.
Dena G
441 PointsDena G
441 PointsGreat! Thank you Robert! I completely missed that. Would you happen to know why when I click on my Add Friend button it displays a blank page instead of my form below? I have the button pointing to:
<%= link_to "Follow", new_user_friendship_path(friend_id: @user), class: "btn btn-info btn-sm" %> Thank you!
user_friendships/new