itx*_*itx 3 ruby-on-rails ruby-on-rails-4
我有控制器看起来像
class BarsController < ApplicationController
after_action :some_method, only: [:index]
def index
get_cache = $redis.get('some_key')
if get_cache.present?
# i want to skip after_action callback in here
else
# other stuff
end
end
end
Run Code Online (Sandbox Code Playgroud)
after_action :some_method
如果get_cache
存在,我该如何跳过?我知道我可以像这样有条件地做到这一点
class BarsController < ApplicationController
after_action :some_method, only: [:index], unless: :check_redis
def index
get_cache = $redis.get('some_key')
if get_cache.present?
# i want to skip after_action callback in here
else
# other stuff
end
end
private
def check_redis
$redis.get('some_key')
end
end
Run Code Online (Sandbox Code Playgroud)
但我认为这是多余的,因为应该多次获得redis.
这应该工作:
class BarsController < ApplicationController
after_action :some_method, only: [:index], unless: :skip_action?
def index
get_cache = $redis.get('some_key')
if get_cache.present?
@skip_action = true
# i want to skip after_action callback in here
else
# other stuff
end
end
private
def skip_action?
@skip_action
end
end
Run Code Online (Sandbox Code Playgroud)
您也可以使用attr_accessor :skip_action
而不是私有方法,因为控制器只是对象.
归档时间: |
|
查看次数: |
404 次 |
最近记录: |