Plugins - UrlForWithPrefix

StarAdd to favorites

Very simple plugin, adds the :prefix option to url_for.

Say in your environment.rb file you’ve got

  ActionController::AbstractRequest.relative_url_root = "/super"

Any time you use link_to, it will prepend /super to the link. This can be a pain if you want to link to other applications:

  link_to "My Other App", :controller => "other_app_controller"
  # => "http://mysite/super/other_app_controller";

You’d like to be able to set a different prefix, or take it out altogether. With this plugin, you can do that:

  link_to "My Other App", :controller => "other_app_controller", :prefix => nil
  # => "http://mysite/other_app_controller";
  link_to "My Other App", :controller => "other_app_controller", :prefix => "/amazing"
  # => "http://mysite/amazing/other_app_controller";

Keep in mind when setting a new prefix, you must have a beginning /, otherwise it will keep the original prefix AND use the new prefix:

  link_to "My Other App", :controller => "other_app_controller", :prefix => "amazing"
  # => "http://mysite/super/amazing/other_app_controller";

Wish I could say I had awesome unit tests, but I don’t. This has worked in all of my apps so far, but I can’t guarantee that it won’t break something, or that a future Rails update won’t break this plugin.

I don’t really understand licenses, so do whatever you want with this, I’d just appreciate a link back to http://www.flpr.org.

Pat Maddox

http://svn.flpr.org/public/plugins/url_for_with_prefix

  • Currently 4.0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Misc. Enhancements

Tags

Comments

Add a comment

Search Plugins

Query syntax

Plugins by Category

Sponsors

Rails Kits: Get Code. Get Moving.

Have a comment?