ITeye博客

在rails console里测试routes

夜鸣猪 · 2012-08-10 10:19 · Ruby On Rails · 编程语言 · Markdown

Sometimes you need to check what your route helpers are returning, and the "rake routes" task isn't enough. Those helpers are accessible through an instance of the ActionController::Integration::Session class, and at the Rails console there is one instance of this class already available:

>> app.class
=> ActionController::Integration::Session
>> app.users_path
=> "/users"

Inside the app object, the host name is automatically prepopulated to "www.example.com":

view sourceprint?

>> app.host
=> "www.example.com"
>> app.users_url
=> "http://www.example.com/users"

You can change that host name easily:

>> app.host = "evolve.st"
=> "evolve.st"
>> app.users_url
=> "http://evolve.st/users"

And remember: That Session instance can be used to simulate HTTP requests too. If you still didn't know about it, you should take a look at its Rails documentation page, because you will find it really interesting.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。