Created
March 3, 2011 20:59
-
-
Save bb/853547 to your computer and use it in GitHub Desktop.
Rails 3 Routing and URL generation challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Index</h1> | |
<p><%= params.inspect%></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Show</h1> | |
<p><%= params.inspect%></p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Expected results of URL generation: