Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Check output of path helper from console

Rails defines a bunch of magic with named routes that make helpers for your routes. Sometimes, especially with nested routes, it can get a little confusing to keep track of what URL you'll get for a given route helper method call. Is it possible to, using the Ruby console, see what link a given helper function will generate? For example, given a named helper like post_path(post) I want to see what URL is generated.

like image 585
Derek Thurn Avatar asked May 17 '10 01:05

Derek Thurn


2 Answers

You can show them with rake routes directly.

In a Rails console, you can call app.post_path. This will work in Rails ~= 2.3 and >= 3.1.0.

like image 90
Chubas Avatar answered Sep 28 '22 15:09

Chubas


you can also

include Rails.application.routes.url_helpers 

from inside a console sessions to access the helpers:

url_for controller: :users, only_path: true users_path # => '/users' 

or

Rails.application.routes.url_helpers.users_path 
like image 31
aghull Avatar answered Sep 28 '22 17:09

aghull