user2821877
user2821877

Reputation: 59

How do I ignore a route in rails?

I have a route i.e. mysite.com:3000/new_route that I'd like to ignore, is this possible to do through rails and not server side?

I've read that this can be done through apache, however, my app is running on Heroku and that type of control isn't accessible to me. Therefore, I'm looking for another way to do this through Rails.

Thanks

update

I am using faye to have live notifications in my app, on localhost faye runs on port 9292 - localhost:9292/faye, all good in development mode, but in production it should point to mydomain.com/faye, there are no port numbers in production environment, and loading faye.js returns error not found

Upvotes: 2

Views: 3095

Answers (3)

Alex Siri
Alex Siri

Reputation: 2864

By the time you ask Rails to process the route, it is already too late. If you ask rails to process a route, it will, either by returning a 404 of 500 error, or a page.

If you want the route to be processed by another application, it will need to be intercepted by your webserver (nginx or apache, or whichever one you're using). In their configuration, you just redirect that route to the other application, and every other route to the Rails app.

EDIT

Another option you have, is to forward your requests to a different server.

You add a route like

get 'faye/*query' => 'faye#get'
post 'faye/*params' => 'faye#post'

And then a controller

require 'faraday'

class FayeController < ApplicationController

  APP = 'http://mydomain.com:9292'

  def get
    request_page :get
  end

  def post
    request_page :post
  end

  private 

  def request_page(method)
    conn = Faraday.new(:url => APP)
    query = params.delete(:query)
    response = conn.send method, query, params
    render text: response.body.gsub(APP, 'mydomain.com/faye')
  end
end

which will use Faraday to load the information from your other application.

Upvotes: 0

Kasperi
Kasperi

Reputation: 863

If you're talking about a resources route you don't want to be created:

resources :something, except: :new

However, I'm not exactly sure if this is what you meant by ignore.

Upvotes: 2

Michał Szajbe
Michał Szajbe

Reputation: 9002

You can define a route at the top of your routes.rb file that will redirect to some other page.

get '/new_route', redirect: '/'

Upvotes: 0

Related Questions