John Merlino
2011-Apr-25 17:57 UTC
perform a check and then call super class when user creates new session
Hey all, I''m using devise plugin for rails. When the user creates a new session, in addition to devise authenticating the user, I want to check if the user is enabled by checking the value of the enabled (a boolean) field in the database. If the value is 0, I want to alert user that they haven''t been enabled, otherwise devise can do it''s thing. I have this: class SessionsController < Devise::RegistrationsController def new super end def create @user = User.authenticate(params[:user][:email]) if !@user.enabled raise "You are not enabled" else super end end def update super end end I have this in User model: class User < ActiveRecord::Base devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable def self.authenticate(email) u = find(:first, :conditions=>["email = ?", email]) return nil if u.nil? return u end scope :enabled, :conditions => {:enabled => true} And the routes: devise_for :users, :controllers => { :registrations => "registrations", :sessions => "sessions" } Unfortunately right now I get a nil object error for instance variable @user, presumably because the params hash is not structured correctly. I look at html form and notice the name attribute for email says users[email] which is an array. So that''s why I try to do it: [:user][:email] but it doesn''t work. Or if you know a better way of doing all this, I''d greatly appreciate the knowledge. Thanks for response. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino
2011-Apr-25 20:51 UTC
Re: perform a check and then call super class when user creates new session
While I almost got the behavior I want, there''s a problem: def create @user = User.authenticate(params[:user]) if !@user.enabled render "shared_navigation/confirmation" elsif request.post? #just using else instead of elsif request.post? gives same unwanted behavior super end end When I click the create button and if the user is enabled, it reloads the same page and I have to click create button twice. This occurs when that super keyword is called which calls the super class. Anyone know why devise reload the same page? Thanks for response. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.