I want to include the routes and the link_to method in a PORO. While testing this in the console I came accross this:
If I include UrlHelper without the routes helper everything seems to work fine:
ruby-1.9.3-rc1 :001 > Rails.version
=> "3.2.0.rc2"
ruby-1.9.3-rc1 :001 > include ActionView::Helpers::UrlHelper
=> Object
ruby-1.9.3-rc1 :002 > link_to "foo", Rails.application.routes.url_helpers.ponies_path
=> "<a href=\"/ponies\">foo</a>"
If I include the routes:
ruby-1.9.3-rc1 :001 > include ActionView::Helpers::UrlHelper
ruby-1.9.3-rc1 :003 > include Rails.application.routes.url_helpers
=> Object
ruby-1.9.3-rc1 :004 > link_to "foo", ponies_path
I get the following error:
NameError: undefined local variable or method `controller' for #<ApplicationController:0x007fa1497ecc40>
What am I doing wrong here?
As suggested you should use the
appobject for the routes part and you should be using thehelperobject for the link_to.No need to include any of the helpers through Ruby when using the console: