Skip to content

Instantly share code, notes, and snippets.

@bb
Created March 3, 2011 20:59
Show Gist options
  • Save bb/853547 to your computer and use it in GitHub Desktop.
Save bb/853547 to your computer and use it in GitHub Desktop.
Rails 3 Routing and URL generation challenge
<!DOCTYPE html>
<html>
<head>
<title>Urltest --- see routes.rb for details</title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
<h3>Menu</h3>
<h4>Individual Pages</h4>
<ol>
<li><%= link_to "Single Post 1 (hardcoded, wanted)", "/1" %></li>
<li><%= link_to "Single Post 1 (generated, actual)", show_path(1) %></li>
<li><%= link_to "Index a (hardcoded, wanted)", "/a" %></li>
<li><%= link_to "Index z (hardcoded, wanted)", "/z" %></li>
<li><%= link_to "Index a, z (hardcoded, wanted)", "/a/z" %></li>
<li><%= link_to "Index a, page 1 (hardcoded, wanted)", "/a/1" %></li>
<li><%= link_to "Index z, page 1 (hardcoded, wanted)", "/z/1" %></li>
<li><%= link_to "Index a, z, page 1 (hardcoded, wanted)", "/a/z/1" %></li>
<li><%= link_to "Index page 1 (hardcoded, wanted)", "/index/1" %></li>
<li><%= link_to "Index (hardcoded, wanted)", "/index" %></li>
<li><%= link_to "Index a (generated with index_path, Good)", index_path(:arg1=>'a') %></li>
<li><%= link_to "Index z (generated with index_path, Good)", index_path(:arg2=>'z') %></li>
<li><%= link_to "Index a, z (generated with index_path, Good)", index_path(:arg1=>'a', :arg2=>'z') %></li>
<li><%= link_to "Index a, page 1 (generated with index_path, Good)", index_path(:arg1=>'a', :page=>'1') %></li>
<li><%= link_to "Index z, page 1 (generated with index_path, Good)", index_path(:arg2=>'z', :page=>'1') %></li>
<li><%= link_to "Index a, z, page 1 (generated with index_path, Good)", index_path(:arg1=>'a', :arg2=>'z', :page=>'1') %></li>
<li><%= link_to "Index (generated with index_path, Good)", index_path %></li>
<li><%= link_to "Index page 1 (generated with index_path, <span style='color:#f00'>BAD: wrong link, doesn't work! It goes to show instead of index</span>)".html_safe, index_path(:page=>'1') %></li>
<li><%= link_to "Index (generated with url_for, Good)", url_for(:action => "index") %></li>
<li><%= link_to "Index a (generated with url_for, <span style='color:#d00'>not good: works but uses query string instead of path</span>)".html_safe, url_for(:action=>"index", :arg1=>'a') %></li>
<li><%= link_to "Index z (generated with url_for, <span style='color:#d00'>not good: works but uses query string instead of path</span>)".html_safe, url_for(:action=>"index", :arg2=>'z') %></li>
<li><%= link_to "Index a, z (generated with url_for, <span style='color:#d00'>not good: works but uses query string instead of path</span>)".html_safe, url_for(:action=>"index", :arg1=>'a', :arg2=>'z') %></li>
<li><%= link_to "Index a, page 1 (generated with url_for, <span style='color:#d00'>not good: works but uses query string instead of path</span>)".html_safe, url_for(:action=>"index", :arg1=>'a', :page=>'1') %></li>
<li><%= link_to "Index z, page 1 (generated with url_for, <span style='color:#d00'>not good: works but uses query string instead of path</span>)".html_safe, url_for(:action=>"index", :arg2=>'z', :page=>'1') %></li>
<li><%= link_to "Index a, z, page 1 (generated with url_for, <span style='color:#d00'>not good: works but uses query string instead of path</span>)".html_safe, url_for(:action=>"index", :arg1=>'a', :arg2=>'z', :page=>'1') %></li>
<li><%= link_to "Index (generated with url_for, Good)".html_safe, url_for(:action=>"index")%></li>
<li><%= link_to "Index page 1 (generated with url_for, Good)", url_for(:action => "index", :page=>'1') %></li>
</ol>
</body>
</html>
<h1>Index</h1>
<p><%= params.inspect%></p>
class PostsController < ApplicationController
end
# The following application has some routing challenges.
# The resolution of requests works great but the generation of the links
# does not always work as intended.
#
# The Question is: How to configure routing that all links that index_path
# or url_for generates come out as the expected ones (see hardcoded version).
# This works well with index_path for all except one link,
# the one which has only a :page, but no args.
#
# Trying to use url_for fixes it for this one case where only :page is set,
# but it breaks it for most other cases.
#
# Environment: Rails 3.0.5
Urltest::Application.routes.draw do
get ':id' => "posts#show", :as => :show, :id => /\d+/
get 'index(/:page)' => "posts#index", :page => /\d+/ # the long path rule
get '(:arg1)(/:arg2)(/:page)' => "posts#index", :as => :index,
:arg1 => /[a-m]/, :arg2 => /[n-z]/, :page => /\d+/ # the short path rule
root :to => "posts#index"
end
<h1>Show</h1>
<p><%= params.inspect%></p>
@bb
Copy link
Author

bb commented Mar 3, 2011

Expected results of URL generation:

list = [
    {}                                                     => "/index", # cannot go to / because / is root
    {:action=>"index", :arg1=>'a'}                         => "/a",
    {:action=>"index", :arg2=>'z'}                         => "/z",
    {:action=>"index", :page=>'1'}                         => "/index/1", # cannot go to /1 because /1 is show
    {:action=>"index", :arg1=>'a', :arg2=>'z'}             => "/a/z",
    {:action=>"index", :arg1=>'a', :page=>'1'}             => "/a/1",
    {:action=>"index", :arg2=>'z', :page=>'1'}             => "/z/1",
    {:action=>"index", :arg1=>'a', :arg2=>'z', :page=>'1'} => "/a/z/1"
  ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment