解决方法
只需简单的添加这个方法到application_controller.rb
protected def authenticate_user! if user_signed_in? super else redirect_to login_path,:notice => 'if you want to add a notice' ## if you want render 404 page ## render :file => File.join(Rails.root,'public/404'),:formats => [:html],:status => 404,:layout => false end end
并且你可以在before_filter上调用这个方法,你想要的另一个控制器.
例如:
class HomesController < ApplicationController before_filter :authenticate_user! ## if you want spesific action for require authentication ## before_filter :authenticate_user!,:only => [:action1,:action2] end
别忘了将login_path添加到routes.rb中
devise_scope :user do match '/sign-in' => "devise/sessions#new",:as => :login end
注意:当我使用设备进行应用程序身份验证时,我总是使用这种方式(rails 3.2和rails 4.0.1)