Rails:如何仅对移动视图应用authenticate_user过滤器

时间:2013-09-09 12:58:41

标签: ruby-on-rails filter

目前,我在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

1 个答案:

答案 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