How to run view helpers from rails console?

With

rails console --sandbox

I cant test my models, save data to db and all will be rolled back after.

But is it possible to test view helpers from console? I talk about these helpers:

post_path link_to url_for edit_post_path new_post_path

When I call them, I get an error: irb(main):003:0> post_path(:post) NameError: undefined local variable or method `post_path' for main:Object

Is there a way to call those methods from Rails console? Through which object do I need to call them?

Test them from tests.

With

rails console --sandbox

I cant test my models, save data to db and all will be rolled back

after.

But is it possible to test view helpers from console? I talk about these

helpers:

post_path

link_to

url_for

edit_post_path

new_post_path

When I call them, I get an error:

irb(main):003:0> post_path(:post)

NameError: undefined local variable or method `post_path’ for

main:Object

Is there a way to call those methods from Rails console? Through which

object do I need to call them?

The routing helpers are all defined on Rails.application.routes.url_helpers

The other helpers are defined on the helper object

helper.number_with_precision(1.1235)

This doesn’t always work - some of the helpers rely on their being a controller/request around and these won’t be able to run.

Fred

i think you can also use app for the url_helpers

app.posts_path for example

Jim ruther Nill wrote in post #1110649:

i think you can also use app for the url_helpers

app.posts_path for example

Oh, yes, this works, they both work. Thank you, that's what I was looking for.

irb(main):005:0> app.posts_path => "/posts" irb(main):006:0> Rails.application.routes.url_helpers => #<Module:0x2d854b8> irb(main):007:0> Rails.application.routes.url_helpers.posts_path => "/posts"