Rails: Mapping Conflicting Routes by Detecting Param

Hey! I am trying to set up routes in a Rails application so that, depending on the type of parameter passed, Rails sends the request to a different action.

I have courses which have an attribute state which is a string with a two letter state abbreviation. When a user visits /courses/1, I want Rails to display the show action in the courses controller (and pass the parameter as :id). When a user visits /courses/CO though, I want Rails to display the index action and pass the parameter as :state.

So /courses/1 would be equivalent to

:controller => 'courses', :action => 'show', :id => '1'

And /courses/CO would be equivalent to

:controller => 'courses', :action => 'index', :state => 'CO'

I have tried this:

map.resources :courses, :except => { :index, :show } map.connect 'courses/:state', :controller => 'courses', :action => 'index', :state => /[A-Z]{2}/ map.connect 'courses/:id', :controller => 'courses', :action => 'show', :id => /[0-9]+/

But it breaks (the rails server wont even start). I don't usually do things like this with routes, so I am outside of my know-how. Thanks!

Edit: Fixed a typo, thanks JC.

Current solution looks like this:

map.resources :courses, :except => [ :index, :show ] map.courses '/courses', :controller => 'courses', :action => 'index', :state => 'AL', :method => :get map.courses '/courses/:state', :controller => 'courses', :action => 'index', :requirements => { :state => /[A-Z]{2}/ }, :method => :get map.course '/courses/:id', :controller => 'courses', :action => 'show', :requirements => { :id => /[0-9]+/ }, :method => :get

-------------Problems Reply------------

This works, but you will need to go edit all your links to the index to say things like courses_path('AA') and you won't be able to use some of the nice helpers, like form_for, which assume you are following the convention that #create is simply #index with a POST request. (Get comfortable with form_tag)

ActionController::Routing::Routes.draw do |map|
map.resources :courses, :except => [ :index, :show ]
map.courses '/courses/:state', :controller => 'courses', :action => 'index', :requirements => { :state => /[A-Z]{2}/ } , :method => :get
map.course '/courses/:id', :controller => 'courses', :action => 'show', :requirements => { :id => /[0-9]+/ } , :method => :get

It will keep your routes named the same, though.

(by the way, your /co does not match your regex, which requires upper case chars)

Fun aside: Do we really need the abstraction of a router? http://blog.peepcode.com/tutorials/2010/rethinking-rails-3-routes

I'm afraid this won't work since the structure that maps paths to controllers and actions is setup on start of the rails application, parameter handling happens at request time.

What you could do is to match the :id-parameter in the show-action of the CoursesController against a list of valid states and then either redirect or render a different action.

Hope this helps.

Category:ruby on rails Views:1 Time:2010-06-29

Related post

  • Rails map.connect route with trailing slash not passing :id to controller (Rails 2.3.5) 2011-07-21

    I have a page that shows our member's profile. Everything works fine if it is called like this www.mysite.com/members/membername but this fails to pass the last segment as params[:id] to the controller when accessed like with a trailing slash: www.my

  • rails form_for and route with nested param 2011-09-12

    Don't really know how to look for this, so without a long search I post this here. What I want is a url like this: /domains/whois/domainname.com The problem is when I use a form the params get in the url like this: /domains/whois/?utf8=✓&whois%5B

  • Dynamic URL -> Controller mapping for routes in Rails 2010-04-07

    I would like to be able to map URLs to Controllers dynamically based on information in my database. I'm looking to do something functionally equivalent to this (assuming a View model): map.route '/:view_name', :controller => lambda { View.find_by_

  • How to map users in root URL in Rails 2.x routes? 2010-11-09

    My routes.rb look like this: #... map.resources :users map.root :controller => "main" #... My app user's profiles are something like: http://www.railsapp.com/users/3 I would like to change it to: http://www.railsapp.com/3 Or even: http://www.rails

  • "A router/access point channel conflict has been detected" error message 2013-12-16

    <Original Title: I'm getting an error message concerning the router/access point between the Epson wireless printer and my computer> The network is working correctly but the message: "A router/access point channel conflict has been detected. ..

  • Can I bind a parameter after an underscore in a rails 2.3 route? 2011-03-24

    In order to maintain backward compatibility, we need to map /sitemap_1234000.xml to the sitemap controller with 1234000 passed as an ID. I tried map.connect '/sitemap_:id.xml', :controller => 'sitemap', :action => 'show' but then when I do asse

  • Rails 3 JQuery update where ".find params[:id]" doesn't work 2011-05-08

    In a rails 3 app i have a form with 20+ questions and answers. The page is populated using a mapped array that has two levels... q[0}.blah is the question array and q[1].yada is the answer array. I am using jQuery to submit each changed answer when a

  • What is the appropriate way to set up a route to transfer params? 2011-01-29

    I am trying to send the current params @calc to a generate_csv action. How can I pass the params, so I can access them within the generate_csv action ? my view - form_tag generate_csv_path(@calc) do = submit_tag 'Print CSV' my routes: match '/generat

  • How do I specify a default in a rails 2.3 route? 2011-03-24

    In my Rails 2.3.11 app, I want to specify that the default format for a route is :xml. According to the documentation I can do this using :defaults map.connect '/myroute', :controller => 'mycontroller', :action => 'myaction', :defaults => {:

  • Rails form_tag redirect route error 2012-02-20

    Hi I have a form_tag that I want to go to a certain action of the controller that I implemented: <%= form_tag(:controller => "admins", :action => "check_in") do %> <%= hidden_field_tag :direction, params[:direction] %> <%= hidden

  • Show Google Static map with route in android 2014-04-09

    I am able to show marker on Google static map but I want to use Google static map with route between two points may any one tell how can i display route on Google static map b/w points In this code the first url is running ok but when i use path url

  • Can I map new routes to the RouteCollection outside Global.asax? 2009-08-05

    I'd like to occasionally map new routes to the RouteCollection during program execution long after the Global.asax RegisterRoutes() method had first executed. How can I do this? --------------Solutions------------- It's possible, but the route collec

  • Map a route to the same controller action 2009-08-07

    I want only one controller action to handle all GETs. How can I map a route to do this? --------------Solutions------------- routes.MapRoute("AllGETs", "{*any}", new { Controller = "YourController", Action = "YourAction" }, new { HttpMethod = new Htt

  • Google Maps : get route, Km, and - price 2009-10-05

    I think I am in a project which is probably too much for me, but I thought you could put me in the right direction. We are trying to have a form where the user will write a starting point and an ending one, as seen in the Google Maps examples (http:/

  • Google Maps V3 Route Destination Mark Edit 2010-03-08

    Using the google.maps.DirectionsService.route() and google.maps.DirectionsRenderer.setDirections() method, is it possible to change the text on the info window for the destination, without creating a custom parser for the journey? I couldn't see anyt

  • RoR: Where is the "rails/info/properties" route defined? 2010-06-13

    I'm running Rails 2.3.4. When I create a new rails project, the public/index.html file has a link named "About your application's environment" that points to "rails/info/properties". In dev mode, it gives a summary of the runtime environment. However

  • Rails 3 - Nested Routes- Help Requested 2010-10-26

    Here is my Rails 3 nested routes structure for PROJECT resources :projects do resources :notes, :photos collection do get 'yourproject', 'newjs' end end This works great for things like /projects /projects/1 /projects/1/notes/ /projects/1/notes/3 wha

  • Mapping a route to a scope as a filter 2010-12-26

    Let's say I have a model called Order with the scopes scope :paid, where("paid_at IS NOT NULL") scope :outstanding, where("paid_at IS NULL") Now I want my app to list all orders (both paid and outstanding) on myapp/orders and to list only the paid or

  • how to map a Route like (site.com/username) in MVC 3 2011-07-02

    I want to map a route like this: mysite.com/username in ASP.NET MVC 3 application; How can I do it, please? Thanks to all, regards --------------Solutions------------- Maybe add custom route at the very beginning that would inherit from System.Web.Ro

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.150 (s). 11 q(s)