The component of the Ruby on Rails framework responsible for mapping between HTTP requests and application resources (including static files and controller actions).
Questions tagged [rails-routing]
969 questions
398
votes
7 answers
Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models?
Say I have a Rails Model called Thing. Thing has a url attribute that can optionally be set to a URL somewhere on the Internet. In view code, I need logic that does the following:
<% if thing.url.blank? %>
<%= link_to('Text', thing_path(thing))…

Aaron Longwell
- 8,153
- 5
- 21
- 10
92
votes
4 answers
Routes with Dash `-` Instead of Underscore `_` in Ruby on Rails
I want my urls to use dash - instead of underscore _ as word separators. For example controller/my-action instead of controller/my_action.
I'm surprised about two things:
Google et al. continue to distinguish them.
That Ruby on Rails doesn't have…

pduey
- 3,706
- 2
- 23
- 31
71
votes
1 answer
Routing with an optional parameter
I added in the route file:
map.show_book "/show_book/:name/year/:year", :controller => "book", :action => "show_version"
I also added:
map.show_book "/show_book/:name", :controller => "book", :action => "show_version"
to show the latest book…

Alessandro De Simone
- 4,085
- 6
- 28
- 41
48
votes
4 answers
rails get app root/base url
In my app I have a few APIs that under api domain. Now in one of the API I want to generate a url that pointing to the main domain, say
test.com/blabla...
I tried to use url_for but seems the default root_url or request.host is in api domain.…

Bruce Lin
- 2,700
- 6
- 28
- 38
46
votes
8 answers
Rails Responds with 404 on CORS Preflight Options Request
I'm creating a set of services using Rails 4, which I am consuming with a JavaScript browser application. Cross-origin GETS are working fine, but my POSTs are failing the preflight OPTIONS check with a 404 error. At least, I think that's what's…

Rolandus
- 758
- 2
- 6
- 12
46
votes
5 answers
How to add custom routes to resource route
I have an invoices_controller which has resource routes. Like following:
resources :invoices do
resources :items, only: [:create, :destroy, :update]
end
Now I want to add a send functionality to the invoice, How do I add a custom route as…

Sathish Manohar
- 5,859
- 10
- 37
- 47
36
votes
7 answers
Adding an action to an existing controller (Ruby on Rails)
I am new to Ruby on Rails, I have completed the Blog Tutorial.
I am now trying to add an additional action to the controller, called 'start'.
def start
end
I have added a view page "app/views/posts/start.html.erb" containing nothing but simple…

Mark
- 16,772
- 9
- 42
- 55
36
votes
1 answer
Rails routing - :on => :collection
The Rails routing guide doesn't specify what :on => :collection means.
I cannot find an explanation to what the :on key is, nor what a :collection is in that context.

Nick Ginanto
- 31,090
- 47
- 134
- 244
34
votes
5 answers
Routing error - uninitialized constant
I could not fix this in Rails 3.2.12, maybe I am missing something.
config/routes.rb
get "home/index"
root :to => "home#index"
devise_for :users, :only => :omniauth_callbacks
match 'users/auth/:provider/callback' => 'authentications#create'
match…

Davit
- 1,394
- 5
- 21
- 47
25
votes
3 answers
Rails routing: resources with only custom actions
I have a NotificationsController, in which I only have the action clear.
I'd like to access this action by doing POST /notifications/clear
So I wrote this in my router:
resources :notifications, :only => [] do
collection do
post :clear
…

Augustin Riedinger
- 20,909
- 29
- 133
- 206
24
votes
6 answers
Listing 'rake routes' for a mountable Rails 3.1 engine
I'm working on a mountable engine for use with Rails 3.1, and I want to list the engine's routes.
I created the engine using:
$ rails plugin new rails_blog_engine --mountable
And edited the 'test/dummy/config/routes' file to…

emk
- 60,150
- 6
- 45
- 50
23
votes
3 answers
How can I make routes from a Rails 3 engine available to the host application?
I have a Rails 3 application with several engines containing additional functionality. Each engine is a separate service that customers can purchase access to.
I am, however, having a problem with routes from the engines that aren't readily…

PerfectlyNormal
- 4,182
- 2
- 35
- 47
23
votes
3 answers
Ruby on Rails: Get the controller and action name based on a path
I am trying to get the controller and action name based on a path. I have a route:
map.resources :permissions
I thought that I could use:
ActionController::Routing::Routes.recognize_path "/permissions/1"
To get a hash like:
{ :controller =>…
please delete me
22
votes
3 answers
How to change a route name rails 4
I changed the routing of posts#index to match blog and I now get /blog in the URL which I was trying to accomplish.
I've tried several different things to get my actual blog post which the route currently looks something like /posts/this-is-a-test…

Kevin Dark
- 2,139
- 5
- 22
- 23
22
votes
6 answers
How can I use Rails routes to redirect from one domain to another?
My app used to run on foo.tld but now it runs on bar.tld. Requests will still come in for foo.tld, I want to redirect them to bar.tld.
How can I do this in rails routes?

John Bachir
- 22,495
- 29
- 154
- 227