有什么方法可以让url_for在动作调度路由期间根据request.host返回url?
mount Collaborate::Engine => '/apps/collaborate', :constraints => {:host => 'example.com' }
mount Collaborate::Engine => '/apps/worktogether'
示例:
当用户在example.com主机上
时collaborate_path => /应用/协作
当用户在任何其他主机上时
collaborate_path => /应用/ worktogether
经过大量研究后,我意识到RouteSet类有name_routes,它不考虑返回url的约束。
我已尝试在action_dispatch / routing / route_set.rb中覆盖@set以从rails应用程序中取出,但dint按预期工作
@search_set = Rails.application.routes.set.routes.select{|x| x.defaults[:host] == options[:host] }[0]
@set = @search_set unless @search_set.blank?
答案 0 :(得分:7)
mCamera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// here we get frame by frame data
}
});
应该正常工作
如果您需要更高级的约束,请制定自己的约束:
mount Collaborate::Engine => '/apps/collaborate', :constraints => {:host => 'examplesite' }
mount Collaborate::Engine => '/apps/worktogether'
您还可以将约束指定为lambda:
class CustomConstraint
def initialize
# Things you need for initialization
end
def matches?(request)
# Do your thing here with the request object
# http://guides.rubyonrails.org/action_controller_overview.html#the-request-object
request.host == "example"
end
end
Rails.application.routes.draw do
get 'foo', to: 'bar#baz',
constraints: CustomConstraint.new
end
来源:http://guides.rubyonrails.org/routing.html#advanced-constraints
答案 1 :(得分:2)
至于我担心如果你在中间件级别处理它那么它会很好。这就是我的假设。
在 config/application.rb
config.middleware.insert_before ActionDispatch::ParamsParser, "SelectiveStack"
在app目录中添加一个中间件,将中间件目录作为约定
<强> app/middleware/selective_stack.rb
强>
class SelectiveStack
def initialize(app)
@app = app
end
def call(env)
debugger
if env["SERVER_NAME"] == "example.com"
"/apps/collaborate"
else
"/apps/worktogether"
end
end
end
希望这能解决您的问题。!!!
答案 2 :(得分:1)
好吧,这是在黑暗中拍摄的;也许你已经尝试过了,或者我真的错过了什么。从表面上看,看起来你只是试图覆盖apps
的路径帮助方法。那么为什么不在application_helper.rb
中设置覆盖呢?类似的东西:
module ApplicationHelper
def collaborate_path
if request.domain == "example.com"
"/apps/collaborate"
else
"/apps/worktogether"
end
end
end