LoginSignup
3
0

More than 3 years have passed since last update.

Rails6 のちょい足しな新機能を試す60(word_wrap編)

Last updated at Posted at 2019-07-31

はじめに

Rails 6 に追加されそうな新機能を試す第60段。 今回は、 word_wrap 編です。
Rails 6 では、 word_wrap 時に元のテキストのインデントが適切に保持されるようになりました。

Ruby 2.6.3, Rails 6.0.0.rc1, Rails 5.2.3 で確認しました。Rails 6.0.0.rc1 は gem install rails --prerelease でインストールできます。

$ rails --version
Rails 6.0.0.rc1

適当な文章を word_wrap で整形して表示してみます。

Controller を作る

Controller と View を作ります。

$ bin/rails g contorller home index

HomeHelper を修正する

HomeHelper module に word_wrap_message を追加します。
word_wrap_message の中で、 word_wrap を使ってメッセージを整形します。

app/helpers/home_helper.rb
module HomeHelper
  def word_wrap_message
    msg = <<~MSG
        This is a multiline message.
        This is a second line of second paragraph. This is a second line part 2
      of second paragraph.
        This is a third paragraph. This line is a part 2 of third paragraph.
    MSG
    word_wrap(msg, line_width: 40)
  end
end

View を修正する

View に word_wrap_messag

app/views/home/index.html.erb
<h1>Home#index</h1>

<pre>
<%= word_wrap_message %>
</pre>

rails server を実行する

$ bin/rails s

ブラウザで確認する

http://localhost:3000/home/index を表示します。

期待通りのインデントで表示されています。
rails6wrap.png

Rails5 では

以下のように表示されてしまいます。
rails5wrap.png

試したソース

試したソースは以下にあります。
https://github.com/suketa/rails6_0_0rc1/tree/try060_word_wrap

その他

文章がおかしいとか間違っているとかそういうところは、優しく見逃してやってください :bow:

参考情報

3
0
1

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
0