Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking if the request is xhr or http from rails routes

I have a get route like below in my routes. How can I check if the request is a xhr or normal http request?

What I want to do is a redirect if its http and if xhr it will go to below controller and action

#if xhr
get '/search' => 'pages#search'

#if http
get '/search' => redirect("/")

how can I do this from routes.rb???

As per BoraMa's answer here is the 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 console request 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

In the rails server log i can see that the request is going to pages#home which is the root.

like image 753
Abhilash Avatar asked May 09 '16 07:05

Abhilash


1 Answers

You can do this using custom routing constraints:

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

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

With this setup, Rails will first call the lambda constraint and will route to the given route only if the constraint results in a trueish value.

like image 65
Matouš Borák Avatar answered Nov 03 '22 22:11

Matouš Borák