带请求子域的条件回调。可能吗?

时间:2017-12-06 09:47:11

标签: ruby-on-rails ruby-on-rails-5.1

我试过这样的事情

class SomeController < ApplicationController
  before_action :something, if: request.subdomain == "specific"


end

但是在这里我无法访问请求对象而且它会抛出错误。

undefined local variable or method `request' for SomeController:Class

有人可以建议我如何实现这个目标吗?

2 个答案:

答案 0 :(得分:1)

使用proc,

class SomeController < ApplicationController
  before_action :something, if: proc { |c| c.request.subdomain == "specific" }


end

答案 1 :(得分:0)

你可以做类似的事情

before_action :something

def something
  if request.subdomain == "specific"
   your code
  end
end