【问题标题】:checking if the request is xhr or http from rails routes检查请求是来自 rails 路由的 xhr 还是 http
【发布时间】:2016-09-03 17:58:15
【问题描述】:

我的路线中有一条get 路线,如下所示。如何检查请求是 xhr 还是普通的 http 请求?

如果它的httpxhr 它将转到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 控制台请求标头

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


【解决方案1】:

您可以使用custom routing constraints

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

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

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

【讨论】:

  • 它适用于非 xhr 路由,它将转到“/”,但对于 xhr 请求,它会转到根控制器和操作...你能告诉我这里做错了什么吗?用完整的 route.rb 更新问题..认为它的一些小错误..
  • 您的路线对我来说似乎是正确的。您确定您实际上是在发出 XHR 请求吗?我用$.get('/search') 很好地测试了它(即在 JS 中,使用 jQuery)。
  • 我正在使用角度,是的,它的 xhr ..如果我只使用 `get '/search' => 'pages#search'`,它会正常工作。我正在使用我为请求获得的标头更新问题。
  • XHR 请求必须有 X-Requested-With header 存在,我在您的标头列表中看不到。抱歉,我现在无法使用 Angular 进行测试,但您可能需要自己将标题添加到 Angular 中,请参阅this 答案。
  • 感谢您指出这一点。没查。但似乎你是对的,因为我没有手动设置标题,因为它在不使用html5Mode 时可以正常工作。我会尝试并在这里更新。我接受这个作为答案,因为它基本上回答了我的问题。感谢您的支持...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多