LoginSignup
3

More than 5 years have passed since last update.

Middlemanテンプレート中でurlエンコード

Last updated at Posted at 2014-06-12

これだと動くっぽいんだけど、激しくタイプ数が多い。。

<%= ERB::Util.u 'http://example.com/' %>
http%3A%2F%2Fexample.com%2F

config.rbでカスタム定義ヘルパにしておけば、単にuと短く書けるようになる。

config.rb
# ↓を追記
helpers do
  def u(text)
    ERB::Util.u(text)
  end
end
# ...
<%# 上記設定がされていれば↓でいける %>
<%= u 'http://example.com/' %>
http%3A%2F%2Fexample.com%2F

うーん、相変わらずRubyの作法がよくわからない。。URLエンコードってよく使いそうだし、もっといいやり方がありそうな気がするんだけどな。

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
3