LoginSignup
1
0

More than 1 year has passed since last update.

【Rails】urlヘルパーでhostの指定

Last updated at Posted at 2021-01-30

urlのヘルパー(こちら)を使うとURLを生成してくれますが、このときホストを指定したいなんてときにどうするかのメモです。

使い方

普通にurlを生成すると以下のようになったとします

hoge_url
# => http://host/hoge

引数でhostを指定するだけです

hoge_url(host: 'fuga')
# => http://fuga/hoge

余談

Railsのコンソールでurlがどのように生成されるか確認する場合は、単にhoge_urlと入力してもうまくいきません

$ rails c
pry(main)> hoge_url
NameError: undefined local variable or method `hoge_url' for main:Object

appを前につければOKです

$ rails c
pry(main)> app.hoge_url
=> "http://www.example.com/hoge"
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0