Rails console with special initial commands
I'm using Rails script/console
to learn REST routes. It is very handful!
In order to do that I need to paste these two lines every开发者_开发技巧 time
I run console:
include ActionController::UrlWriter
default_url_options[:host] = 'whatever'
If there is any way to make a script which will add this automatically every time I run console?
You can use the ActionController::Integration::Session object that is available at the console with the name “app”:
>> app.blog_path
=> "/blog"
One thing to note is that the app object's host name is automatically prepopulated to “www.example.com”:
>> app.host
=> "www.example.com"
>> app.blog_url
=> "http://www.example.com/blog"
A bit of a hack but you could put two calls in a .rb
file and require
that when you start the console.
E.g.
i.rb
include ActionController::UrlWriter
default_url_options[:host] = 'whatever'
$ ruby script/console
>> require 'i'
精彩评论