开发者

Devise custom sign_in error

I have a custom SessionsController using Devise, when i browse /users/sign_in and enter some authdata then submit, have some errors.

NoMethodError in Users::SessionsController#create

undefined method `serialize_into_session' for Symbol:Class
Rails.root: /home/kewang/rails/devis开发者_高级运维etest

Application Trace | Framework Trace | Full Trace
devise (1.4.5) lib/devise/rails/warden_compat.rb:19:in `serialize'
warden (1.0.5) lib/warden/session_serializer.rb:25:in `store'
warden (1.0.5) lib/warden/proxy.rb:161:in `set_user'
devise (1.4.5) lib/devise/controllers/helpers.rb:111:in `sign_in'
app/controllers/users/sessions_controller.rb:3:in `create'

app/controllers/users/sessions_controller.rb

class Users::SessionsController < Devise::SessionsController
  def create
    sign_in(resource_name, resource)
  end
end

app/helpers/application_helper.rb

module ApplicationHelper
  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

config/routes.rb

devise_for :users, :controllers => { :sessions => "users/sessions" }

what can i do?

My development enviroment is Rails 3.1.0, Ruby 1.9.2


Devise has a bunch of custom methods that it uses in its controllers.

You could try calling super to get the methods, or

class Users::SessionsController < Devise::SessionsController
  def create
    sign_in(resource_name, resource)
    super
  end
end

But I would recommend including the whole Devise Sessions#create method, then changing things from that base. Link here

def create
  self.resource = warden.authenticate!(auth_options)
  set_flash_message(:notice, :signed_in) if is_navigational_format?
  sign_in(resource_name, resource)
  respond_with resource, :location => after_sign_in_path_for(resource)
end


What you want is

sign_in :user

I think the common mistake is to create a user, say with a Factory, and try

let(:user) { create(:user) }

...and then call sign_in with current_user as a string:

sign_in user

..which results in the dreaded "serialize_in_session" error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜