Trung Tran
Trung Tran

Reputation: 13721

Record current user ruby on rails 4.0

I have an Opportunity model that has a nested resource of many Updates. At the moment, my application is able to log in/out users and record their session Id's. One of the fields in my Update model is "made_by". I am trying to create a feature where the application will automatically record and store the email of a person that made that update. For example, if I am logged [email protected] and I make an update, the "made_by" attribute for that update will be [email protected]. The error I am getting is "undefined method `email' for nil:NilClass". Here is my current code:

Update model:

class Update < ActiveRecord::Base


    validates_presence_of :description
    validates_presence_of :update_date
    belongs_to :opportunity
    before_save :update_made_by

    def update_made_by
        made_by = @current_user.email #<-- error is located here
    end

end

Update Controller:

def create

@opportunity = Opportunity.find(params[:opportunity_id])
@current_user = User.find(session[:user_id]) if session[:user_id] #<--this is where I define "current_user"
@update = @opportunity.updates.new(update_params)
  if @update.save
    redirect_to @opportunity, notice: 'Update has been added'
  else
    redirect_to @opportunity, alert: 'Unable to add Update'
  end
end

Application Controller:

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception
  helper_method :current_user

  private

  helper_method :current_user

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def authenticate
    logged_in? || access_denied
  end

  def logged_in?
    current_user.is_a? User
  end

  helper_method :logged_in?

  def access_denied
    redirect_to :controller => 'sessions', :action => 'new', notice: "Please log in to continue"
    return false
  end

end

I would like to mention that current_user.email works fine in my views/layouts/application.html.erb:

Logged in as <%= current_user.email %>.

Can someone please help? Thank you!!

Upvotes: 1

Views: 148

Answers (2)

spickermann
spickermann

Reputation: 106792

The instance variable @current_user and the method current_user is only available in the scope of the controller (and its helpers). When you want to use that users in a model, then you have to pass that value to the model.

Most simple way to do so is by changing the following line in the controller:

@opportunity.updates.new(update_params.merge(:made_by => current_user.email))

And than remove the update_made_by method from your model.

Upvotes: 3

Doon
Doon

Reputation: 20232

change

def update_made_by
    made_by = @current_user.email #<-- error is located here
end

to be

def update_made_by
    made_by = current_user.email #<-- error is located here
end

Since @current_user isn't defined in the model, only in the controller . You can also remove the @current_user from the create method as it isn't needed.

and in this case do you want to just store the email of the current user or would you rather link it to the current user. so that down the road you can do something like user.updates to get all the updates that the user had.

Upvotes: 0

Related Questions