Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 4, subdomain routing
    text
    copied!<p>Trying to implement web service in rails through API sub-domain called "api".<br> In my <code>hosts</code> file i added line: <code>127.0.0.1 api.localhost</code></p> <p>In my <code>routes.rb</code> i set sub-domain, where i only need index action and few manually added restful routes, through following:</p> <pre><code>namespace :api, path: '', :constraints =&gt; {:subdomain =&gt; "api"} do resources :posts, only: :index do collection do get 'popular_by_day' get 'popular_by_week' end end end </code></pre> <p>Also generated coresponding controller with: <code>rails g controller api/posts</code></p> <p><strong>Test example:</strong></p> <pre><code>class Api::PostsController &lt; ApplicationController def index @posts = 1 respond_to do |format| format.json { render :json =&gt; @posts } end end def popular_by_day end def popular_by_week end end </code></pre> <p>After <code>rake routes</code> i have following: <br></p> <pre><code>popular_by_day_api_posts GET /posts/popular_by_day(.:format) api/posts#popular_by_day {:subdomain=&gt;"api"} popular_by_week_api_posts GET /posts/popular_by_week(.:format) api/posts#popular_by_week {:subdomain=&gt;"api"} api_posts GET /posts(.:format) api/posts#index {:subdomain=&gt;"api"} </code></pre> <p>So far as i know, link to <a href="http://api.localhost:3000/posts" rel="noreferrer">http://api.localhost:3000/posts</a> should work but i get routing error: <br><strong>No route matches [GET] "/posts"</strong> (Same with /posts.json)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload