目前,我在SomethingController
中获得了以下代码:
class SomethingController < ApplicationController
skip_filter :authenticate_user!, :only => [:new, :create, :edit, :update]
#...
#new
#create
#edit
#update
end
目前:我们希望未经身份验证的用户能够创建或更新Something
个对象。
问题:由于我们的手机身份验证性质不同,我们希望限制未经身份验证的手机用户在登录/注册之前无法使用此控制器操作。我们可以在过滤器中添加一些条件,例如:
skip_filter :authenticate_user!, :only => [:new, :create, :edit, :update], :format=>:html
skip_filter :authenticate_user!, :only => [], :format=>:mobile
如果不可能,最佳做法是什么?这可以接受吗?
def new
if current_user.nil?
#redirect to sign_in/up actions
end
#rest of the method
end
答案 0 :(得分:1)
仅针对非移动请求跳过过滤器。像下面的东西。
class SomethingController < ApplicationController
skip_filter :authenticate_user!, :only => [:new, :create, :edit, :update], :unless => :mobile?
#...
#new
#create
#edit
#update
def mobile?
#implementation here depends on how you do the mobile detection
end
end