Creating a Friend While On Their Profile Page-Collection of common programming errors
I want a User(x) to be able to add another User(y) as a friend while User(x) is on User(y’s) Profile Page. I set up a has_many_through and everything works except that I can only add a friend from the User Index View. Thank you in advance…The code is below:
Also:
I wanted to place the “friend” link on the view/profile/show.html.erb. When I added @users = User.all to the existing profiles_controller.rb I received the error – undefined method friendships' for nil:NilClass. When I replaced @user = User.find(params[:id]) with @users = User.all I received the error - NoMethodError in Profiles#show... undefined method
inverse_friends’ for nil:NilClass
The Code that works in UserIndexView but not ProfileShowView:
% for user in @users %>
user), :method => :post%>
The following error occurs:
NoMethodError in Profiles#show
Showing /Users/mgoff1/LOAP_1.2.2/app/views/profiles/show.html.erb where line #13 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #13):
10:
11:
12:
13:
14:
15:
16:
. . .
app/views/profiles/show.html.erb: 13:in`_app_views_profiles_show_html_erb___2905846706508390660_2152968520'
app/controllers/profiles_controller.rb:19:in `show'
The code to the rest is below.
friendship.rb
class Friendship < ActiveRecord::Base
attr_accessible :create, :destroy, :friend_id, :user_id
belongs_to :user
belongs_to :friend, :class_name => "User"
end
user.rb
class User < ActiveRecord::Base
has_many :friendships
has_many :friends, :through => :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user
# 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, :profile_attributes
# attr_accessible :title, :body
has_one :profile
accepts_nested_attributes_for :profile
before_save do | user |
user.profile = Profile.new unless user.profile
end
end
friendships_controller.rb
class FriendshipsController < ApplicationController
def create
@friendship = current_user.friendships.build(:friend_id => params[:friend_id])
if @friendship.save
flash[:notice] = "Added friend."
redirect_to current_user.profile
else
flash[:error] = "Unable to add friend."
redirect_to root_url
end
end
def destroy
@friendship = current_user.friendships.find(params[:id])
@friendship.destroy
flash[:notice] = "Removed friendship."
redirect_to current_user.profile
end
end
users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def index
@users = User.all
end
end
profiles_controller.rb
class ProfilesController < ApplicationController
# GET /profiles
# GET /profiles.json
def index
@profiles = Profile.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @profiles }
end
end
# GET /profiles/1
# GET /profiles/1.json
def show
@user = User.find(params[:id])
@profile = Profile.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @profile }
end
end
# GET /profiles/new
# GET /profiles/new.json
def new
@profile = Profile.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @profile }
end
end
# GET /profiles/1/edit
def edit
@user = User.find(params[:id])
@profile = Profile.find(params[:id])
end
# POST /profiles
# POST /profiles.json
def create
@profile = Profile.new(params[:profile])
respond_to do |format|
if @profile.save
format.html { redirect_to @profile, notice: 'Profile was successfully created.' }
format.json { render json: @profile, status: :created, location: @profile }
else
format.html { render action: "new" }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# PUT /profiles/1
# PUT /profiles/1.json
def update
@profile = Profile.find(params[:id])
respond_to do |format|
if @profile.update_attributes(params[:profile])
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1
# DELETE /profiles/1.json
def destroy
@profile = Profile.find(params[:id])
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url }
format.json { head :no_content }
end
end
end
routes.rb
BaseApp::Application.routes.draw do
resources :friendships
resources :profiles
#get "users/show"
devise_for :users, :controllers => { :registrations => "registrations" }
resources :users
match '/show', to: 'profile#show'
match '/signup', to: 'users#new'
root to: 'static_pages#home'
match '/', to: 'static_pages#home'
. . .
-
You aren’t setting
@users
inProfilesController#show
.for object in collection
just callscollection.each do |object|
, which is why you’re gettingundefined method 'each' for NilClass
(and also why it’s generally discouraged to use that syntax, as it creates confusing errors like this one).profiles_controller.rb
def show @users = User.all #... end
-
Anytime you try to call methods with no actual object you’ll get the ‘method undefined’.
It means that the method IS defined – but you have a ‘nil’ and are trying to call it on that and that method doesn’t exists for the ‘nil’ object.
Please check your actual users table. You’ll need users to work with. Please verify that you have some.
If necessary you can create users (at the
script/rails console
) withUser.new(:name=>'fred', :password =>'pword', :password_confirmation => 'pword' )
You can also place this in your
db/seeds.db
file so you can runrake db:seed
the first time you set the application up on a new machine.
Originally posted 2013-11-09 22:47:04.