gpt4 book ai didi

ruby-on-rails - 检查请求是来自 rails 路由的 xhr 还是 http

转载 作者:行者123 更新时间:2023-12-03 15:59:50 26 4
gpt4 key购买 nike

我的 route 有一条如下所示的 get 路线。如何检查请求是 xhr 还是正常的 http 请求?

我想要做的是重定向,如果它的 http 并且如果 xhr 它将转到 controlleraction 下面

#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 控制台请求 header
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,它是根。

最佳答案

您可以使用 custom routing constraints :

get '/search', to: 'pages#search',
constraints: lambda { |request| request.xhr? }

get '/search', to: redirect("/"),
constraints: lambda { |request| !request.xhr? }

通过这种设置,Rails 将首先调用 lambda 约束,并且仅当约束产生一个真实值时才会路由到给定的路由。

关于ruby-on-rails - 检查请求是来自 rails 路由的 xhr 还是 http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37110017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com