我正在尝试根据用户是否是机器人来提供不同的页面,所以我将此代码放在routes.rb文件中:
request.env["HTTP_USER_AGENT"].include? "bot"
并收到此错误:
block in <top (required)>': undefined local variable or method `request' for #<ActionDispatch::Routing::Mapper:0x007fe30f329f98> (NameError)
任何想法如何使我的工作?
环顾四周找到了这个,但它仍然给我一个错误:
constraints :user_agent => /bot/ do
root :to => "events#index"
end
constraints :user_agent => /^((?!bot).)*$/ do
root :to => "main#index"
end
答案 0 :(得分:2)
我想你想要routing constraints。
例如:
# Routes matched in order specified, so this will be checked first
constraints :user_agent => /bot/ do
root :to => "events#index", as: :bot_root
end
# If the above route didn't apply, this one should happen instead
root :to => "main#index"
as: :bot_root
应该有助于避免根命名冲突
答案 1 :(得分:0)