🎉

Railsのlink_toヘルパーで絶対パスが出力されない

2024/02/15に公開

起こった問題

# controller
@domain = 'example.net'

# view

= link_to @domain, @domain

原因

  • link_toで渡すURLがhttp/httpsが含まれない場合、相対パスとして認識されてしまう
    • localhost:3000/example.net

対策

  • http or httpsを後付けする
= link_to @domain, "https://#{@domain}"
  • そもそもデータを持つ際に、validationやらでURLにしておいたほうがいいかもしれない。

Discussion