Rails routes for an API documentation - ruby-on-rails

I'm currently writing the documentation for the API of my website.
I'm not sure about the "best" way to write the routes. I think twitter does a good job and I'd like to copy their url structure:
https://dev.twitter.com/docs/api
https://dev.twitter.com/docs/api/1/get/statuses/show/:id
https://dev.twitter.com/docs/api/1/post/statuses/retweet/:id
It would be something like:
namespace :docs do
resources :api do
# and then... not sure
end
end
Not sure on how to write the routes for this part: /get/statuses/show/:id.
Should I just create a custom route?
match "/:verb/:resource/:action/:params" => "api#resource"
Or is there a better way?
What I ended up with, might help someone :)
Ibarcraft::Application.routes.draw do
def api_versions; [:v1] end
def api_verbs; [ :index, :show ] end
constraints subdomain: "api" do
scope module: "api", as: "api" do
versions = api_versions
versions.each do |version|
namespace version, defaults: { format: "json" } do
# all my routes
resources :barcrafts, only: api_verbs do
collection do
get :search
end
scope module: "barcraft" do
resources :users, only: [:index]
end
end
# and more...
end
end
match 'v:api/*path', to: redirect { |params, request| "/#{versions.last}/#{params[:path]}" + (params[:format] ? ".#{params[:format]}" : "") }
match '*path', to: redirect { |params, request| "/#{versions.last}/#{params[:path]}" + (params[:format] ? ".#{params[:format]}" : "") }
end
end
end

Yes you need custom route.
But note, that :action key is reserved for controller's action name, better use something like :action_name

Related

Custom Routing in Rails 5

I'm having some issues with custom routing. What I'm looking to do is remove the model from the route and dynamically use the record name.
so instead of:
site.com/events/my-event
I would like it to be:
site.com/my-event
I hacked this to work with the below code, only issue is I can't access my admin namespace as it's being treated as an event record (and any other route):
get('/:id', to: redirect do |params, request|
id = request.path.gsub("/", "")
"/events/#{id}"
end)
I know this redirect is not right, I'm just not well versed in routing options. How should this be done properly?
routes.rb
Rails.application.routes.draw do
resources :events, param: :id, path: "" do
get 'login', to: 'sessions#new', as: :login
get 'logout', to: 'sessions#destroy', as: :logout
post 'sessions', to: 'sessions#create', as: :session_create
end
namespace 'admin' do
root "events#index"
resources :sessions, only: [:create]
get 'login', to: 'sessions#new', as: :login
get 'logout', to: 'sessions#destroy', as: :logout
resources :events
end
end
Rails lets you specify what a routing URL should look like:
https://guides.rubyonrails.org/routing.html#translated-paths
You don't need redirect.
You need to match all requests to one controller/action.
get '/(*all)', to: 'home#app'
Inside this action check what is coming in params and render appropriate view.
PS: it will capture all requests, even for not found images, js, etc.
(1) To fix /admin to go to the right path, move the namespace line above the events resources so that it is matched first:
Rails routes are matched in the order they are specified 2.2 CRUD, Verbs, and Actions
# routes.rb
namespace :admin do
root "events#index"
#...
resources :events
end
resources :events, param: :name, path: ""
Use :param and :path to match for site.com/myevent instead of site.com/events/:id
Your events controller:
# events_controller.rb
class EventsController < ApplicationController
# ...
private
def set_event
#event = Event.find_by(name: params[:name])
end
end
Your admin events controller:
# admin/events_controller.rb
class Admin::EventsController < ApplicationController
# ...
private
def set_event
#event = Event.find params[:id]
end
end
TIP: To get a complete list of the available routes use rails routes in your terminal 5.1 Listing Existing Routes

Subdomain based resources in Rails

I am looking for a simple way to replace all my routes matching mydomain.com/resources/xx by xx.mydomain.com with Rails 4.
Would anyone have an idea to do that easily and that would work with nested resources as well?
Thanks, Joris
Constraints
What you're looking for is constraints in your routes, specifically that you're looking to use one to determine whether you have a subdomain that you can access
There are a number of resources on how to achieve this:
Basecamp-style subdomains by DHH
Subdomains Railscast
The bottom line is that you'll probably have to create a custom subdomai constraint, which you can then use the standard routing structure for:
#lib/subdomain.rb
class Subdomain
def self.matches?(request)
if request.subdomain.present? && request.subdomain != 'www'
account = Account.find_by username: request.subdomain
return true if account # -> if account is not found, return false (IE no route)
end
end
end
#config/routes.rb
constraints(Subdomain) do
get "/", to: "controller#action"
resources :posts #-> subdomain.domain.com/posts
...
end
The above is untested - I also found the following with Rails' documentation:
#lib/subdomain.rb
class Subdomain
def initialize
#accounts = Account.all
end
def matches?(request)
if request.subdomain.present? && request.subdomain != 'www'
#accounts.include?(request.subdomain)
end
end
end
#config/routes.rb
constraints: Subdomain.new do
get "/", to: "controller#action"
resources :posts #-> subdomain.domain.com/posts
...
end
Here how I have done it before in a Rails 3 app:
constraints :subdomain => /ambassador/ do
namespace(:influencer, :path => '/') do
root :to => 'home#index'
match 'home' => 'sweepstakes#index', :as => :influencer_home
resources :sweepstakes
resources :associates
resources :widgets
resources :sessions
resources :reports do
resource :member
end
match 'faq' => 'info#faq'
end
end
Be sure to put this block towards the top of the routes.rb file so it takes precedence.
You can of course nest your resources in here like normal.

Remove controller from friendly URL

I have written freindly URLs for the show action of the School Resource but now have
before i had ;
http://webaddress/schools/2
and now i have;
http://webaddress/schools/school_name
However, i want
http://webaddress/school_name
My config routes look like this for the resource;
resources :schools do
collection do
match 'search' => 'schools#search', via: [:get, :post], as: :search
end
end
How can i achieve that? thank you
Add this at the last your routes file:
match ':id' => 'schools#show', via: [:get]
A more conventional way would be to use the path: option in your route resources, like this:
#config/routes.rb
...
resources :schools, path: "", only: :show #-> has to go at end of file!
This will give you the ability to add different methods to this, as well as keeping with Rails conventions :)

How to build SEO urls in Rails 3.x with lots of optional URL params?

Ok, I'm trying to make my urls seo friendly and get a little more indexing juice from google. Basically, I have some urls that look like this:
/articles?page=2&filter=all
and I want it to look like this
/articles/all/2
I've got the /artiles/:filter/:page part to work just fine as I did my routes like this:
resources: :articles do
get '(:filter(/:page)', action: :index, on: :collection
end
My question is how do I get the page param to work without the filter (or other optional params)?
/articles/?page=2
should look like
/articles/2
I've been thinking of using contraints but can't seem to get it to work, something along the lines of this
resources: :articles do
get ':page', action: :index, on: :collection, constraints: { page: /\d+/ }
get '(:filter(/:page)', action: :index, on: :collection
end
EDIT
I didn't realize it but the above works, what doesn't is the link_to is not generating the pretty urls. e.g. /articles/all/1, it's still outputting /articles?filter=all. This is the link_to code I'm using:
= link_to "Filter", articles_path(filter: 'all') #=> /articles?filter=all
and I want: /articles/all and /articles/all/2 and /articles/2 and to all work.
Why not add the word "page" to the URL so the routing helper knows it's calling a page and not passing a filter name:
/articles/page/1
In routes.rb
resources :articles do
get 'page/:page', action: "index", on: :collection, constraints: { page: /\d+/ }, as: :articles_with_page
get ':filter/page/:page', action: :index, on: :collection, as: :articles
end
The brackets mean that that part of the route name is optional. Since in you're case you're being quite specific about the pattern it should match, It'd remove them.
Also, you've called the filter namespace :articles - that will clash with the default index route so you should either rename it or pass except: [:index] to the resources block:
resources except: [:index] do
# stuff here
end
Hope that helps
This isn't pretty but it might put you in the right direction.
module ApplicationHelper
def articles_with_optional_page_path(params)
if params[:page] && !!params[:filter]
articles_with_page_path(params)
else
articles_path(params)
end
end
end
resources: :articles do
get ':page', action: :index, on: :collection, constraints: { page: /\d+/ }, as: :articles_with_page
get '(:filter(/:page)', action: :index, on: :collection, as: :articles
end

Route a controller to namespace :admin to /admin

I feel like this may be a dumb question, but it's late and my head is melting a bit.. So I appreciate the assistance.
I'm trying to map the url http://localhost:3000/admin to a dashboard controller but i'm epically failing. Maybe this isn't even possible or the completely wrong idea but anyway my routes looks like this and yes
namespace :admin do
resources :dashboard, { :only => [:index], :path => '' }
...
end
and my simple dashboard_controller.rb
class Admin::DashboardController < ApplicationController
before_filter :authenticate_user!
filter_access_to :all
def index
#schools = School.all
end
end
and my view is located in views/admin/dashboard/index.html.erb
thanks for any input
If all you're trying to do is route /admin to that dashboard controller, then you're overcomplicating it by namespacing it like that.
Namespacing with a nested resource like that would mean that it would be /admin/dashboards for the :index action instead of having a clean /admin route (and you can verify that by running rake routes at the command line to get a list of your routes).
Option 1: You meant to namespace it like that
# putting this matched route above the namespace will cause Rails to
# match it first since routes higher up in the routes.rb file are matched first
match :admin, :to => 'admin/dashboards#index'
namespace :admin do
# put the rest of your namespaced resources here
...
end
Option 2: You didn't mean to namespace it like that
Route:
match :admin, :to => 'dashboards#index'
Controller:
# Remove the namespace from the controller
class DashboardController < ApplicationController
...
end
Views should be moved back to:
views/dashboards/index.html.erb
More info: http://guides.rubyonrails.org/routing.html
Regarding to http://guides.rubyonrails.org/routing.html I prefer this
namespace :admin do
root to: "admin/dashboards#index"
resources :dashboard
end
Try this:
namespace :admin do
root to: 'users#index' # whatever. Just don't start with /admin
#resources :dashboards <= REMOVE THIS LINE !
end

Resources