user3494239
user3494239

Reputation: 45

Rails: Undefined method `id' for nil:NilClass error

I am currently trying to make a tennis website using RoR. This website has a backend and currently has two tables in the database, users and events. I am trying to make a has_many relationship between the two tables and then make a form that lets the users sign up for these different events. I am pretty close but I keep running into this error in my view:

    undefined method `id' for nil:NilClass

   <%= link_to event.title, event %>
  | <%= link_to "delete", event, method: :delete %>

   <%= form_for(current_user.relationships.build(event_id: @user.id)) do |f| %>
   <div><%= f.hidden_field :event_id %></div>
     <%= f.submit "Sign up", class: "btn btn-large btn-primary" %>
   <% end %>

Here is my relationships controller

    class RelationshipsController < ApplicationController

      def create
       @relationship = Relationship.new(relationship_params)
       @user = User.find(params[:relationships][:event_id])
       current_user.follow!(@user)
       respond_to do |format|
       format.html { redirect_to @user }
       end
     end
    end

And the relevant part of my events controller:

   def new
     @event = Event.new
   end

   private

   def event_params
     params.require(:event).permit(:title, :Venue, :Time, :Date)
   end

Not sure how to fix this error, any help would be greatly appreciated.

Upvotes: 4

Views: 25288

Answers (3)

Promise Preston
Promise Preston

Reputation: 29078

I had a similar issue when working on a Rails application. The issue was that I was calling an unnecessary instance variable in my controller.

Here's it:

I was calling an unnecessary @admin instance variable in log_in @admin

sessions/admins_controller.rb

def create
  user = User::Admin.find_by(email: params[:sessions_admin][:email].downcase)
  if user && user.authenticate(params[:sessions_admin][:password])
    log_in @admin
    format.html { redirect_to users_admin_url(user), notice: 'Admin was successfully logged in.' }
    format.json { render :show, status: :created, location: users_admin_url(user) }
  else
    format.html { render :new }
    format.json { render json: @admin.errors, status: :unprocessable_entity }
  end
end

But error message was being flagged by rails in a corresponding helper file that I included in my application_controller file.:

sessions/admins_helper.rb

module Sessions::AdminsHelper

  # Logs in the given user.
  def log_in(user)
    session[:user_id] = user.id
  end
end

I simply had to modify the method call in the create action in sessions/admins_controller.rb from:

log_in @admin

to

log_in user

That's all.

I hope this helps

Upvotes: 0

Teguh Syahmar
Teguh Syahmar

Reputation: 136

You should initialize the @user variable

def new
  @user = User.new
  @event = Event.new
end

Upvotes: 4

crispychicken
crispychicken

Reputation: 2662

In your controller new action you have to define @user. Currently this variable is empty. So if you call the id method on something that's not existent you'll get the error undefined method id for Nil...

For example like this:

def new
  @user = User.find(1)
  @event = Event.new
end

Upvotes: 1

Related Questions