Skip to content

Instantly share code, notes, and snippets.

@epochwolf
Created October 4, 2010 01:45
Show Gist options
  • Save epochwolf/609136 to your computer and use it in GitHub Desktop.
Save epochwolf/609136 to your computer and use it in GitHub Desktop.
# route
get "flag/:root_type/:root_id" => 'flags#show', :as => :flag
#linking to it
link_to "Flag this", flag_path("journal", @journal.id)
# the :as option in the route generates a method named "#{option[:as]}_path"
# this method has two ways to call it
# in sequence
flag_path("journal", 4)
# as a hash
flag_path(:root_type => "journal", :root_id => 4)
# they both generate
"/flag/journal/4"
# any additional params that aren't recognized are tacked onto the url like so
flag_path("journal", 4, :blue => 4) #=> "/flag/journal/4?blue=4"
flag_path(:root_type => "journal", :root_id => 4, :blue => 4, :red => 3) #=> "/flag/journal/4?blue=4&red=3"
#UNLESS they are options for url_for (look up url_for in the rails api)
flag_path("journal", 4, :trailing_slash => true) #=> "/flag/journal/4/"
flag_path("journal", 4, :trailing_slash => true, :blue => 4) #=> "/flag/journal/4/?blue=4"
#nested resources get really funky with this which is beyond this explaination, just look up polymorphic_url_for instead of url_for
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment