Skip to content

Instantly share code, notes, and snippets.

@nuxlli
Created July 10, 2012 17:43
Show Gist options
  • Save nuxlli/3085010 to your computer and use it in GitHub Desktop.
Save nuxlli/3085010 to your computer and use it in GitHub Desktop.
To access url helpers (url_for, etc) from Rails console (Rails 3)
# Example from: http://snipplr.com/view/37063/
include Rails.application.routes.url_helpers
# set host in default_url_options:
default_url_options[:host] = "localhost"
# can then use:
url_for()
# can check existing routes:
edit_user_url(User.first)
=> "http://localhost/user/1/edit"
@Schwad
Copy link

Schwad commented Sep 1, 2017

In Rails 5 universe I would do the same but instead of just url_for() do:

ActionController::UrlFor.url_for(Document.last.attachment)

@iHiD
Copy link

iHiD commented Mar 24, 2020

Thanks :)

@arkalgorepublic
Copy link

The above did not work for me exactly.
Actually line 6 is supposed to be like this:
default_url_options[:host] = "localhost:3000"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment