简体   繁体   中英

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.

Eg

i.rb
  include ActionController::UrlWriter
  default_url_options[:host] = 'whatever'

$ ruby script/console
>> require 'i'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM