Questions tagged [rails-routing]

The component of the Ruby on Rails framework responsible for mapping between HTTP requests and application resources (including static files and controller actions).

955 questions
385
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
  • 7,963
  • 5
  • 19
  • 10
88
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,414
  • 2
  • 21
  • 28
67
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…
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…
42
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 Xinda Lin
  • 2,470
  • 6
  • 24
  • 37
40
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,015
  • 8
  • 31
  • 44
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,042
  • 9
  • 40
  • 55
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,340
  • 5
  • 20
  • 47
32
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
  • 26,414
  • 39
  • 123
  • 214
23
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
  • 56,152
  • 6
  • 40
  • 49
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…
22
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
2 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 …
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
  • 21,401
  • 22
  • 137
  • 203
20
votes
4 answers

How can I create a Rails 3 route that will match all requests and direct to one resource / page?

I have a rails app (Rails 3.0) that I need to temporarily take out of service. While this is in effect, I want to create a new route that will direct all requests to a single piece of static content. I have a controller set up to serve my static…
Nick
  • 949
  • 1
  • 9
  • 14
1
2 3
63 64