Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4, subdomain routing

Trying to implement web service in rails through API sub-domain called "api".
In my hosts file i added line: 127.0.0.1 api.localhost

In my routes.rb i set sub-domain, where i only need index action and few manually added restful routes, through following:

namespace :api, path: '', :constraints => {:subdomain => "api"} do
  resources :posts, only: :index do
    collection do
      get 'popular_by_day'
      get 'popular_by_week'
    end
  end
end

Also generated coresponding controller with: rails g controller api/posts

Test example:

class Api::PostsController < ApplicationController
  def index
    @posts = 1

    respond_to do |format|
        format.json  { render :json => @posts }
    end
  end

  def popular_by_day
  end

  def popular_by_week
  end
end

After rake routes i have following:

popular_by_day_api_posts GET  /posts/popular_by_day(.:format)  api/posts#popular_by_day {:subdomain=>"api"}
popular_by_week_api_posts GET  /posts/popular_by_week(.:format) api/posts#popular_by_week {:subdomain=>"api"}
                api_posts GET  /posts(.:format)                 api/posts#index {:subdomain=>"api"}

So far as i know, link to http://api.localhost:3000/posts should work but i get routing error:
No route matches [GET] "/posts" (Same with /posts.json)

like image 934
Srle Avatar asked Nov 20 '13 11:11

Srle


People also ask

What is Tld_length?

The tld_length parameter is used in splitting HOST into domain and subdomain components. Unfortunately, @@tld_length is used in other functions, so to be thread safe you would have to find and rewrite all those functions as well as provide thread-local storage.

What are RESTful routes in Rails?

Rails RESTful Design REST is an architectural style(Wikipedia) that is used in Rails by default. As a Rails developer, RESTful design helps you to create default routes by using resources keyword followed by the controller name in the the routes.rb file resources :users.

How do I find routes in Rails?

TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.


3 Answers

So I'm posting my research on the topic

By default, Rails has its TLD Length set to 1. As a result, it won't be able to determine the subdomain in api.localhost, hence, the routing error.

You have several solutions:
- set up a correct domain that points to 127.0.0.1 with a tld length of 1 (e.g. dev-mywebsite.com)
- use lvh.me
- set config.action_dispatch.tld_length = 0 in your development.rb enrironment file

Hope this helps other people in the same need

Edit: It is actually not a good idea to use localhost so setup an api as a domain in development. I found out that it doesn't work with cookies across subdomains

like image 120
Brent Avatar answered Oct 19 '22 01:10

Brent


Try http://api.lvh.me:3000for Rails 3+

From Ryan Bates's railscasts:

If we go to http://lvh.me:3000/ we’ll see the homepage of our application because lvh.me resolves to the IP address 127.0.0.1. The difference is that we can now prepend any subdomain we like to the URL and it will still point to the same application.

EDIT: I would love to know in the comments why whoever downvoted this answer did so. This approach continues to work for me.

EDIT II: updated the link to Rails Casts from ASCII casts and added note about Rails 3+

like image 44
maxbeizer Avatar answered Oct 18 '22 23:10

maxbeizer


@Srle It seems, I faced this yesterday.

Just try to add ".com" to your subdomain: api.localhost.com.

In my app it worked as well: api.myapplication-dev.com.

The main reason is that there isn't a .localhost domain, so put .com to your host :)

Hope it works for you as worked for me

like image 2
Gil Gomes Avatar answered Oct 19 '22 01:10

Gil Gomes