{"id":812,"date":"2022-08-30T15:07:35","date_gmt":"2022-08-30T15:07:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/creating-a-friend-while-on-their-profile-page-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:07:35","modified_gmt":"2022-08-30T15:07:35","slug":"creating-a-friend-while-on-their-profile-page-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/creating-a-friend-while-on-their-profile-page-collection-of-common-programming-errors\/","title":{"rendered":"Creating a Friend While On Their Profile Page-Collection of common programming errors"},"content":{"rendered":"<p>I want a User(x) to be able to add another User(y) as a friend while User(x) is on User(y&#8217;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&#8230;The code is below:<\/p>\n<p>Also:<\/p>\n<p>I wanted to place the &#8220;friend&#8221; link on the view\/profile\/show.html.erb. When I added @users = User.all to the existing profiles_controller.rb I received the error &#8211; undefined method <code>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<\/code>inverse_friends&#8217; for nil:NilClass<\/p>\n<p>The Code that works in UserIndexView but not ProfileShowView:<\/p>\n<pre><code>% for user in @users %&gt;\n    \n    <br \/>\n      <strong> <\/strong>\n       user), :method =&gt; :post%&gt;\n    \n    <br \/><br \/>\n    \n\n<\/code><\/pre>\n<p>The following error occurs:<\/p>\n<pre><code>    NoMethodError in Profiles#show\n    Showing \/Users\/mgoff1\/LOAP_1.2.2\/app\/views\/profiles\/show.html.erb where line #13 raised:\nundefined method `each' for nil:NilClass\nExtracted source (around line #13):\n10: \n11: \n12: \n13:   \n14:       \n15:         <br \/>\n16:           <strong> <\/strong>\n    . . .\n\napp\/views\/profiles\/show.html.erb:    13:in`_app_views_profiles_show_html_erb___2905846706508390660_2152968520'\napp\/controllers\/profiles_controller.rb:19:in `show'\n<\/code><\/pre>\n<p>The code to the rest is below.<\/p>\n<p>friendship.rb<\/p>\n<pre><code>class Friendship &lt; ActiveRecord::Base\nattr_accessible :create, :destroy, :friend_id, :user_id\n\n belongs_to :user\n belongs_to :friend, :class_name =&gt; \"User\"\nend\n<\/code><\/pre>\n<p>user.rb<\/p>\n<pre><code>    class User &lt; ActiveRecord::Base\n  has_many :friendships\n  has_many :friends, :through =&gt; :friendships\n  has_many :inverse_friendships, :class_name =&gt; \"Friendship\", :foreign_key =&gt;   \"friend_id\"\n  has_many :inverse_friends, :through =&gt; :inverse_friendships, :source =&gt; :user\n\n  # Include default devise modules. Others available are:\n  # :token_authenticatable, :confirmable,\n  # :lockable, :timeoutable and :omniauthable\n  devise :database_authenticatable, :registerable,\n     :recoverable, :rememberable, :trackable, :validatable\n\n  # Setup accessible (or protected) attributes for your model\n  attr_accessible :email, :password, :password_confirmation, :remember_me,    :profile_attributes\n  # attr_accessible :title, :body\n\n\n\n  has_one :profile\n  accepts_nested_attributes_for :profile\n\n  before_save do | user |\n  user.profile = Profile.new unless user.profile\n end\nend\n<\/code><\/pre>\n<p>friendships_controller.rb<\/p>\n<pre><code>class FriendshipsController &lt; ApplicationController\n  def create\n    @friendship = current_user.friendships.build(:friend_id =&gt; params[:friend_id])\n    if @friendship.save\n      flash[:notice] = \"Added friend.\"\n      redirect_to current_user.profile\n    else\n      flash[:error] = \"Unable to add friend.\"\n      redirect_to root_url\n    end\n   end\n\n  def destroy\n    @friendship = current_user.friendships.find(params[:id])\n    @friendship.destroy\n    flash[:notice] = \"Removed friendship.\"\n    redirect_to current_user.profile\n  end\n end\n<\/code><\/pre>\n<p>users_controller.rb<\/p>\n<pre><code>class UsersController &lt; ApplicationController\n  def show\n    @user = User.find(params[:id])\n  end\n\n  def index\n    @users = User.all\n  end\nend\n<\/code><\/pre>\n<p>profiles_controller.rb<\/p>\n<pre><code>class ProfilesController &lt; ApplicationController\n  # GET \/profiles\n  # GET \/profiles.json\n  def index\n    @profiles = Profile.all\n\n    respond_to do |format|\n      format.html # index.html.erb\n      format.json { render json: @profiles }\n    end\n  end\n\n  # GET \/profiles\/1\n  # GET \/profiles\/1.json\n  def show\n    @user = User.find(params[:id])\n    @profile = Profile.find(params[:id])\n\n    respond_to do |format|\n      format.html # show.html.erb\n      format.json { render json: @profile }\n    end\n  end\n\n  # GET \/profiles\/new\n  # GET \/profiles\/new.json\n  def new\n    @profile = Profile.new\n\n    respond_to do |format|\n      format.html # new.html.erb\n      format.json { render json: @profile }\n    end\n  end\n\n  # GET \/profiles\/1\/edit\n  def edit\n    @user = User.find(params[:id])\n    @profile = Profile.find(params[:id])\n  end\n\n  # POST \/profiles\n  # POST \/profiles.json\n  def create\n    @profile = Profile.new(params[:profile])\n\n    respond_to do |format|\n      if @profile.save\n         format.html { redirect_to @profile, notice: 'Profile was successfully created.' }\n         format.json { render json: @profile, status: :created, location: @profile }\n      else\n        format.html { render action: \"new\" }\n         format.json { render json: @profile.errors, status: :unprocessable_entity }\n      end\n    end\n  end\n\n  # PUT \/profiles\/1\n  # PUT \/profiles\/1.json\n  def update\n    @profile = Profile.find(params[:id])\n\n    respond_to do |format|\n      if @profile.update_attributes(params[:profile])\n        format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }\n        format.json { head :no_content }\n      else\n        format.html { render action: \"edit\" }\n        format.json { render json: @profile.errors, status: :unprocessable_entity }\n      end\n    end\n  end\n\n  # DELETE \/profiles\/1\n  # DELETE \/profiles\/1.json\n  def destroy\n    @profile = Profile.find(params[:id])\n    @profile.destroy\n\n    respond_to do |format|\n      format.html { redirect_to profiles_url }\n      format.json { head :no_content }\n    end\n  end\n end\n<\/code><\/pre>\n<p>routes.rb<\/p>\n<pre><code>BaseApp::Application.routes.draw do\n\n\n  resources :friendships\n\n  resources :profiles\n\n  #get \"users\/show\"\n\n  devise_for :users, :controllers =&gt; { :registrations =&gt; \"registrations\" }\n  resources :users\n\n  match '\/show', to: 'profile#show'\n\n\n\n\n  match '\/signup',  to: 'users#new'\n\n  root to: 'static_pages#home'\n\n  match '\/', to: 'static_pages#home'\n\n  . . .\n<\/code><\/pre>\n<ol>\n<li>\n<p>You aren&#8217;t setting <code>@users<\/code> in <code>ProfilesController#show<\/code>.<\/p>\n<p><code>for object in collection<\/code> just calls <code>collection.each do |object|<\/code>, which is why you&#8217;re getting <code>undefined method 'each' for NilClass<\/code> (and also why it&#8217;s generally discouraged to use that syntax, as it creates confusing errors like this one).<\/p>\n<p><strong>profiles_controller.rb<\/strong><\/p>\n<pre><code>def show\n  @users = User.all\n  #...\nend\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Anytime you try to call methods with no actual object you&#8217;ll get the &#8216;method undefined&#8217;.<\/p>\n<p>It means that the method IS defined &#8211; but you have a &#8216;nil&#8217; and are trying to call it on that and that method doesn&#8217;t exists for the &#8216;nil&#8217; object.<\/p>\n<p>Please check your actual users table. You&#8217;ll need users to work with. Please verify that you have some.<\/p>\n<p>If necessary you can create users (at the <code>script\/rails console<\/code>) with<\/p>\n<p><code>User.new(:name=&gt;'fred', :password =&gt;'pword', :password_confirmation =&gt; 'pword' )<\/code><\/p>\n<p>You can also place this in your <code>db\/seeds.db<\/code> file so you can run <code>rake db:seed<\/code> the first time you set the application up on a new machine.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:47:04. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I want a User(x) to be able to add another User(y) as a friend while User(x) is on User(y&#8217;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&#8230;The code is below: Also: I wanted to place the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-812","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=812"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/812\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}