[英]checking if the request is xhr or http from rails routes
我有一個get
在我的路線像下面的路線。 如何檢查請求是xhr還是正常的http請求?
我想要做的是重定向,如果它的http
和xhr
它將轉到controller
和action
之下
#if xhr
get '/search' => 'pages#search'
#if http
get '/search' => redirect("/")
我怎么能從routes.rb
做到這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服務器日志中,我可以看到請求將轉到作為根的#home頁面。
您可以使用自定義路由約束執行此操作:
get '/search', to: 'pages#search',
constraints: lambda { |request| request.xhr? }
get '/search', to: redirect("/"),
constraints: lambda { |request| !request.xhr? }
使用此設置,Rails將首先調用lambda約束,並且僅當約束導致true值時才會路由到給定路由。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.