views:

196

answers:

2

Hi. I'm new to rails and getting the following error:

NameError in FriendshipsController#create  
uninitialized constant FriendshipsController

this also shows up:

{"authenticity_token"=>"eQvv3flATE+P1TEErOWP/6fM8dEOIBxltobCxtM/F18=", "friend_id"=>"32"}

When I click on the "Add Friend" Link on my users show page. I am following the railscast about self referential associations to a T, but I keep getting this error and I can't find any information about it, not even what "uninitialized constant" means. I've gathered from the internet that it MAY be related to the acts_as_authenticated plugin, but I followed the one fix I found and it didn't work.

Here is the code from my user/show.html.erb page:

<%= link_to "Add Friend", friendships_path(:friend_id => @user.id), :method => :post %> 

and the code from my friendships controller:

def create
  @friendship = current_user.friendships.build(:friend_id => params[:friend_id])
  if @friendship.save
    flash[:notice] = "Added friend."
    redirect_to root_url
  else
    flash[:error] = "Unable to add friend."
    redirect_to root_url
  end
end

Where am I going wrong here. I haven't the faintest clue what is causing this. Please let me know if I am missing any needed code.

A: 

Rails is complaining because you have used a constant before initializing it.

puts SomeConstant
# before
SomeConstant = 10

In this case the constant is a controller Class Name - FriendshipsController

Check if the class name is correct, i.e. you have a controller with that name in your app\controller directory.

Gishu
+1  A: 

Difficult to tell. You should post the top part of your class... requires, class definition, includes, and anything else you have that is outside of your methods, as well as the create method.

Lonny Eachus
that got it. For some reason I needed to change the filename to "friendships_controller.rb" as it generated as "friendship_controller.rb" for some reason. I also had to change the line "class FriendshipController < ApplicationController" to "class FriendshipsController < ApplicationController". Hmm I must have messed up when generating.
Ryan Max