undefined method `user_id' for nil:NilClass | Devise and Rails 4

304 views
Skip to first unread message

Deepak Sharma

unread,
May 19, 2016, 1:52:43 PM5/19/16
to rubyonra...@googlegroups.com
In my application I want only current user to edit and delete his/her stuff and for others stuff he just able to show it. I'm using devise with rails 4.

Here my files.

Migration

rails g migration AddUserIdToStudents user:references

user.rb

class User < ActiveRecord::Base
  has_many :students
end

student.rb

class Student < ActiveRecord::Base
  belongs_to :user
end

student_controller.rb

class StudentsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_student, only: [:show, :edit, :update, :destroy]

  def index
  end

  # Branch wise students
  def mechanical
    @students = Student.where(Branch: "Mechanical Engineering")
  end
  def infomation_technology
    @students = Student.where(Branch: "Information Technology")
  end

  def new
    @student = current_user.students.build
  end

  def edit
  end

  def create
    @student = current_user.students.build(student_params)

    respond_to do |format|
      if @student.save
        format.html { redirect_to @student}
        format.json { render :show, status: :created, location: @student }
      else
        format.html { render :new }
        format.json { render json: @student.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    def set_student
      @student = Student.find(params[:id])
    end

    def student_params
      params.require(:student).permit( :University_Roll_Number, :avatar, :Department_Type, :user_id)
    end
end

_actions.html.erb

<% @students.each do |student| %>
  <div class="card-action">
    <div class="center-align">

      <!-- If Statement -->

      <% if current_user.try(:admin?) %>
        <%= link_to student, :class=> "btn waves-light waves-effect grey darken-4" do %>
          <span class="button-name">Show</span>
        <% end %>
        <%= link_to edit_student_path(student), :class=> "btn waves-light waves-effect grey darken-4" do %>
          <span class="button-name">Edit</span>
        <% end %>
        <%= link_to student, method: :delete, data: { confirm: 'Are you sure?' }, :class=> "btn waves-light waves-effect grey darken-4" do %>
          <span class="button-name">Delete</span>
        <% end %>

      <!-- Else If Statement -->

      <% elsif @student.user_id == current_user.id %>
        <%= link_to student, :class=> "btn waves-light waves-effect grey darken-4" do %>
          <span class="button-name">Show</span>
        <% end %>
        <%= link_to edit_student_path(student), :class=> "btn waves-light waves-effect grey darken-4" do %>
          <span class="button-name">Edit</span>
        <% end %>
        <%= link_to student, method: :delete, data: { confirm: 'Are you sure?' }, :class=> "btn waves-light waves-effect grey darken-4" do %>
          <span class="button-name">Delete</span>
        <% end %>

      <!-- Else Statement -->

      <% else %>
        <%= link_to student, :class=> "btn waves-light waves-effect grey darken-4" do %>
          <span class="button-name">Show</span>
        <% end %>
      <% end %>
    </div>
  </div>
<% end %>
 

With all that configuration I'm getting following error  undefined method `user_id' for nil:NilClass. In my students table user_id is passing but still facing this error. Where am I doing wrong.

--
Cheers!

Deepak Kumar Sharma
Guru Nanak Dev Engineering College
India!

Blog: http://deekysharma.wordpress.com

Colin Law

unread,
May 19, 2016, 4:21:44 PM5/19/16
to Ruby on Rails: Talk
On 19 May 2016 at 18:52, Deepak Sharma <deeky....@gmail.com> wrote:
> In my application I want only current user to edit and delete his/her stuff
> and for others stuff he just able to show it. I'm using devise with rails 4.
> ...
>
> With all that configuration I'm getting following error undefined method
> `user_id' for nil:NilClass. In my students table user_id is passing but
> still facing this error. Where am I doing wrong.

Initially what you are doing wrong is not posting the full error
message and not telling us which line of code the error refers to.
The error means that you have tried to call something.user_id but the
value of 'something' is nil. Look at the line of code the error
refers to and try to work out why the variable is nil.

Colin

Deepak Sharma

unread,
May 19, 2016, 4:58:46 PM5/19/16
to rubyonra...@googlegroups.com

On Fri, May 20, 2016 at 1:50 AM, Colin Law <cla...@gmail.com> wrote:
>
>
> Initially what you are doing wrong is not posting the full error
> message and not telling us which line of code the error refers to.
> The error means that you have tried to call something.user_id but the
> value of 'something' is nil.  Look at the line of code the error
> refers to and try to work out why the variable is nil.

I'm getting an error message because of following statement


<!-- Else If Statement -->
<% elsif @student.user_id == current_user.id %>
  ...
<% end %>

@student.user_id not working. But in DB id's are not empty and user_id also exists in student's table.


If I go like this

<% @students.each do |student| %>
  <% elsif  student.user_id == current_user.id %>
  ...
  <%end %>
<% end %>

Intially everything woks fine. Current user able to edit, delete and show his profile and other user's able to show it only (i.e only show button appear). Else and Elsif statements works fine. But when another user add his data and after saving he's able to edit and delete current user' data.


Case 1: (Current user add his data)

Current User:

Show | Delete | Edit

Another User:

Show (Current User profile)


Case 2: (When another user enters his data and after saving it)

Current User:

Show |  Edit | Delete

Another User:

Show  | Edit | Delete (Current User profile)

Colin Law

unread,
May 19, 2016, 5:11:12 PM5/19/16
to Ruby on Rails: Talk
On 19 May 2016 at 21:58, Deepak Sharma <deeky....@gmail.com> wrote:
>
> On Fri, May 20, 2016 at 1:50 AM, Colin Law <cla...@gmail.com> wrote:
>>
>>
>> Initially what you are doing wrong is not posting the full error
>> message and not telling us which line of code the error refers to.
>> The error means that you have tried to call something.user_id but the
>> value of 'something' is nil. Look at the line of code the error
>> refers to and try to work out why the variable is nil.
>
> I'm getting an error message because of following statement
>
> <!-- Else If Statement -->
>
> <% elsif @student.user_id == current_user.id %>
> ...
> <% end %>
>
> @student.user_id not working. But in DB id's are not empty and user_id also
> exists in student's table.

Note that here you have put @student.user_id

>
>
> If I go like this
>
> <% @students.each do |student| %>
> <% elsif student.user_id == current_user.id %>

Also note that her you have said student.user_id (no '@')
In the code you posted initially it is @student, which is nil as you
have not assigned a value to it.

Colin

Deepak Sharma

unread,
May 19, 2016, 5:25:24 PM5/19/16
to rubyonra...@googlegroups.com
On Fri, May 20, 2016 at 2:40 AM, Colin Law <cla...@gmail.com> wrote:
>
> Note that here you have put @student.user_id

<snip>

> Also note that her you have said student.user_id (no '@')
> In the code you posted initially it is @student, which is nil as you
> have not assigned a value to it.

Yes, I tried to different approach with @student I'm getting an error
of undefined 'user_id' and with student.user_id things work but in
abrupt manner.

And I figured out where I'm commit mistake.

Actually my index.html.erb has partial and that partial has sub-partial.

Index.html.erb
|--Partial File
|--- Partial File

And in both partial files, in order to access data i'm passing each loop i.e

<% @students.each do |student| %>
<% end %>

Now I deleted sub-partial and put all content in single partial file.

Index.html.erb
|--- Partial File

Now everything working fine.
Reply all
Reply to author
Forward
0 new messages