【发布时间】:2016-09-03 17:58:15
【问题描述】:
我的路线中有一条get 路线,如下所示。如何检查请求是 xhr 还是普通的 http 请求?
如果它的http 和xhr 它将转到controller 和action 下方,我想要做的是重定向
#if xhr
get '/search' => 'pages#search'
#if http
get '/search' => redirect("/")
我怎么能从routes.rb做到这一点???
根据 BoraMa 的回答,这里是 routes.rb
Rails.application.routes.draw do
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
devise_for :users,
:path => '',
:path_names => {:edit => 'profile'},
:controllers => {:registrations => 'registrations'}
resource :pages, only: [:edit] do
collection do
patch 'update_password'
end
end
resources :conversations, only: [:create, :index] do
resources :messages, only: [:create, :index]
end
resources :notifications do
collection do
post :mark_as_read
end
end
root 'pages#home'
get '/general' => 'pages#general'
get '/drafts' => 'rooms#drafts'
get '/active' => 'rooms#active_listings'
get '/inactive' => 'rooms#inactive_listings'
get '/search', to: 'pages#search',
constraints: lambda { |request| request.xhr? }
get '/search', to: redirect("/"),
constraints: lambda { |request| !request.xhr? }
get '/change_password' => 'pages#change_password'
# get ':page' => 'pages#show'
resources :rooms do
post :make_active
post :make_inactive
resources :photos, only: [:index, :create, :destroy]
end
mount ImageUploader.direct_endpoint, at: "/attachments/images"
end
chrome 控制台请求标头
Request URL:http://localhost:3000/search.json?latitude=12.9715987&longitude=77.59456269999998
Request Method:GET
Status Code:301 Moved Permanently
Remote Address:127.0.0.1:3000
Response Headers
view source
Cache-Control:no-cache
Content-Length:88
Content-Type:text/html
Location:http://localhost:3000/
X-Request-Id:5686e56f-07af-44f5-a1a3-518bb47b513c
X-Runtime:0.006135
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:_deploy_test_session=L3IwZUxYNVRITGpPYlE1OHZoMmFVdHJqRjRlRGpKb2xDaEhWTzNjZ0RkY01nNGVjeUlZVUlHckN3L0FGcTZuVXp6ckF0a1R6ZEt5alJzZ3VPcjFVNWdqZjJUeG1DaVhXNXlXaGhwVWd2UHF4NldYMzYxU0oxeVRmZFV5UTk1YUJHZnF3WTVNa0FuVWcwZFdqN1dUMXh3PT0tLVZ0Q0FqSkNXYmtKL25HRHJzWC9VOFE9PQ%3D%3D--7cfd795484ed3f9139e850399405698e0a33e308; XSRF-TOKEN=JBQC%2BanEl82RonPiLTdzSIMjkbtItgavnYtaYdIQ15MFXBVNUDS7gP5pxLkWV3zwagKypjcV23J2soOLdxhvtQ%3D%3D; _roomduck_session=ZjVhalZIdW9Ma2NZNnJ5cHIxY3hudDhCRFpBT1pwWEc5WjN2TkxzcEZaQTVYZ0RpUzBEVTFVWWFxbGJ5NTVkRWNJcDF6Qkg0dFRicVIwYTlCSG5WcDEvSXZ6aW9ZcUNzL0lPaGpRbUg5Z3dJVE1GcnQxSWo0V1hZOG9OUzNaZmYwQ0wwZ3k2M0xMd0RKVWNTK2VWOHVqNm1EQjhvd045dEg5ejRqNEtFWGhYSVN0ZzRpSDR2OU5udFRzOEdKZnBMY3RiVklDdnRGQ0lEbUJsbnhWcENjVlhISHB4dzZtS05XRmpwVndlZUpxdz0tLUhieEJGT0RZdWxaREZrckZyWlRkZnc9PQ%3D%3D--0ad068283b426d9158fe4c74d5abc6e9eab52bb9
Host:localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36
X-XSRF-TOKEN:JBQC+anEl82RonPiLTdzSIMjkbtItgavnYtaYdIQ15MFXBVNUDS7gP5pxLkWV3zwagKypjcV23J2soOLdxhvtQ==
Query String Parameters
view source
view URL encoded
latitude:12.9715987
longitude:77.59456269999998
在 Rails 服务器日志中,我可以看到请求将转到 pages#home,它是根目录。
【问题讨论】:
-
为什么不在控制器中处理呢?
-
@Nithin 在前端使用 Angular,我所需要的只是一个简单的重定向..认为在路由中执行会更有效..难道路由不可能吗??
-
@Nithin 所以不可能..
标签: ruby-on-rails