dexter00x
dexter00x

Reputation: 948

undefined method `session' for ApplicationController:Class

I am practicing with rails and I was in the topic of "session" and I get the message "undefined method `session' for ApplicationController:Class"

please help me

this is the code *(controller aplication)

class ApplicationController < ActionController::Base
  session :session_key => 'ruby_cookies'
end

*(controller when I want to create the cookies)

class RegistroController < ApplicationController
  def index
  end

  def login
    if request.post?
      p_user = User.new(params[:user])
      user   = User.find_by_nombre_and_password(p_user.nombre, p_user.password)
      if user
        session[:user_id] = user.id
        flash[:notice]    = "se ha identificado correctamente"
        redirect_to home_url
      else
        flash[:notice] = "se incorrecto psps"
        redirect_to login_url
      end
    end
  end

  def logout
    session[:user_id] = nil
    flash[:notice] = "adios sayonara"
    redirect_to home_url
  end
end

Upvotes: 0

Views: 3240

Answers (2)

trptcolin
trptcolin

Reputation: 2340

Your code in ApplicationController doesn't belong there. It belongs in a configuration file, for example config/environment.rb, where it would read something like this:

config.action_controller.session = {     
  :session_key => 'ruby_cookies'
}

See http://guides.rubyonrails.org/configuring.html for much more detail.

Upvotes: 0

Peter Brown
Peter Brown

Reputation: 51717

Your code is really hard to read, but the issue is probably related to this line where it looks like it's trying to call a method "session" and pass it a key/value pair.

session :session_key => 'ruby_cookies'

This doesn't appear to be within any sort of controller action. Normally you would set a session value with session[:my_value] = 'value' and read it with session[:my_value], just like a normal hash.

Upvotes: 1

Related Questions