boulder_ruby
boulder_ruby

Reputation: 39695

Getting RuntimeError: "In order to use #url_for, you must include routing helpers explicitly" when I've already included them

Inside a controller I've tried to run this code for when users that are already logged in stumble across the sign up page

def index
  if current_user
    redirect_to homebase_url #should provide url to home for logged in users
  end
end

I've done what the rails error message said and have added: include Rails.application.routes.url_helpers to the containing controller class. Still getting this error though. Definitely do not want to hardcode URLs into there for legacy purposes. Thanks

Upvotes: 4

Views: 938

Answers (2)

Blair Anderson
Blair Anderson

Reputation: 20171

I recommend using homebase_path instead of homebase_url

For example when looking at http://stackoverflow.com/questions/41903124/getting-runtimeerror-in-order-to-use-url-for-you-must-include-routing-helper/42048113#42048113

console.log(window.location.pathname) // => "/questions/41903124/getting-r..."
console.log(window.location.host) // => "stackoverflow.com"

path is the stuff after the domain name(host).

a complete URL(in your case homebase_url) requires both a host and a path. In development its a bit awkward to get a host because its a different value in production than it is in development.

Upvotes: 1

Shannon
Shannon

Reputation: 3018

Remove the include Rails.application.routes.url_helpers declaration, it's not needed unless it's in something like a helper. Routes are included in controllers by default. Including it in a model or controller/initializer (routes are loaded before initializers) is against MVC architecture and might cause unwanted behaviour.

Upvotes: 3

Related Questions