18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ControllerなどView以外からurl_helpers(_url,_path)を呼ぶ

Last updated at Posted at 2012-05-11
routes = Rails.application.routes.url_helpers 
routes.hoge_article_path

とか。ただし文脈によっては_urlは使えず、以下のようなエラーが出る。

05:17:40:pry % routes.api_hoge_fuga_url
ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
from /path/to/app/vendor/bundle/ruby/1.9.1/gems/actionpack-3.1.0/lib/action_dispatch/http/url.rb:25:in `url_for'

この場合は、上記エラー文にあるとおり

05:17:51:pry % routes.api_hoge_fuga_url(host: 'homu.local')
=> "http://homu.local/api/hoge/fuga"

こうすれば使える。

18
17
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
18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?