Rails设计了未定义的方法`after_sign_in_path_for'

时间:2016-03-23 09:45:14

标签: ruby-on-rails devise

我有控制器

class SessionsController < Devise::SessionsController
  respond_to :js
  layout false

  def create
    self.resource = warden.authenticate(auth_options)

    if resource && resource.active_for_authentication?
      sign_in(resource_name, resource)
    end
  end

end

和我的模板

create.js.erb

<% if user_signed_in?%>
  <%= after_sign_in_path_for(resource) %>
<% else %>
 Erorrs here...
  })
<% end %>

我希望在登录后重定向到特定页面但是已经

NoMethodError - undefined method `after_sign_in_path_for' for #<#<Class

我应该怎么做才能解决它?

  Rails.application.routes.draw do
      devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks",:confirmations => "confirmations",:passwords => "passwords", :sessions => "sessions" }
  end

1 个答案:

答案 0 :(得分:0)

我有类似的实现可以试试这个

class Devise::SessionsController < DeviseController
  prepend_before_filter :require_no_authentication, :only => [ :new, :create ]

    def create
      self.resource = warden.authenticate(auth_options)

      if resource && resource.active_for_authentication?
        sign_in(resource_name, resource)
      end

      respond_with resource, :location => after_sign_in_path_for(resource)
    end
end

ApplicationController

private

  def after_sign_in_path_for(resource)
    stored_location_for(resource) || request.referer || root_path
  end