Old Emmanuel Oga's Weblog (new one is at www.emmanueloga.com)

Using link_to, url_for, path methods from arbitrary places

Posted in rails, rspec, ruby by emmanueloga on mayo 30, 2008

This is one thing I always forget, so I will post it to have it as reference.

If you need to call url_for or path methods (e.g.: root_path) from outside a controller, a view or a helper (perhaps inside a class lying somewhere inside RAILS_ROOT/lib/**/*), you will need to:

include ActionController::UrlWriter

If you also want to use link_to, you will need to:

include ActionView::Helpers::UrlHelper
include ActionController::UrlWriter

I include the two modules as I will probably want to use path methods in my link_to. Also notice that the UrlWriter module comes after the UrlHelper module.

“But it does not work!” I can hear you scream… If you get a message like this:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.url_for
(eval):17:in `project_stories_path'

Fear not, you may not have included the files in the right order. That message will appear if you include ActionController::UrlWriter before including ActionView::Helpers::UrlHelper. Just stick to the order I show above and the message will disappear.

5 comentarios

Subscribe to comments with RSS.

  1. Rob Dupuis said, on julio 1, 2008 at 8:30 pm

    You need to be careful with this. It can cause urls to render differently even if rendered from your views.

    Specifically, if you access a page at an ip address eg.

    http://192.168.1.4:3000/blah

    url_for(…other…) called from a view should render as http://192.168.1.4:3000/other, but actually renders as http://localhost:3000/other

    This can be a problem for xss. Not sure why this happens, but just caused me a lot of pain.

  2. Nate Clark said, on septiembre 24, 2008 at 6:19 pm

    Thanks so much, you saved me hours of debugging! All I had to do was switch the order of the two includes as you have shown. Sweet!

  3. Joey said, on noviembre 2, 2008 at 6:19 pm

    To use link_to successfully in my controller, I also needed the following include:

    include ActionView::Helpers::TagHelper

    Without it, I received an “undefined method tag_options” error.

  4. Matthijs said, on enero 13, 2009 at 10:33 am

    Great info, too bad the _path doesn’t work.?

    I cant find any method_missing? in those helpers that catch the _path methods..

  5. Paul said, on mayo 12, 2009 at 10:18 pm

    I echo Rob’s warning. This can cause some really weird bugs that can be next to impossible to debug.


Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: