Skip to content

Commit

Permalink
Merge pull request #117 from kazu-0226/feature/hyperlink
Browse files Browse the repository at this point in the history
[Fix]#115/ハイパーリンクの受信側の表示エラー
  • Loading branch information
kazu-0226 authored Oct 20, 2020
2 parents f253597 + 3123b29 commit e4878e4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
8 changes: 5 additions & 3 deletions app/helpers/chat_messages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module ChatMessagesHelper
require "uri"

def content_url_to_link(content)
# 左右のパーシャルで呼び出すため、contentが二重に書き換えられるのを防ぐため、dupメソッドでコピーしてcontentを複数用意する
dup_content = content.dup
# URI.extractで引数の[“http”, “https”].uniqで引数のcontentが「httpかhttps」のいずれかで始まるテキスト要素を重複を削除した形で配列を生成
URI.extract(content, ["http", "https"]).uniq.each do |url|
URI.extract(dup_content, ["http", "https"]).uniq.each do |url|
# 「gsub!」メソッドでcontentをの値を”< a href=”#{url}”target=”_blank”>#{url}</a>”に置換
content.gsub!(url, "<a href='#{url}' target='_blank'>#{url}</a>")
dup_content.gsub!(url, "<a href='#{url}' target='_blank'>#{url}</a>")
end
content
dup_content
end
end
2 changes: 2 additions & 0 deletions app/views/chat_messages/_chat_message_left.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<% end %>
</div>
<div class="says">
<%# binding.pry if content.match(/^http/) %>
<strong><%= content_url_to_link(content).html_safe %></strong> <br>

<%= created_at.strftime("%Y-%m-%d %H:%M")%>
</div>
</div>
1 change: 0 additions & 1 deletion app/views/chat_messages/_chat_message_right.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<% end %>
</div>
<div class="mycomment">
<%# binding.pry if content.match(/^http/) %>
<strong><%= content_url_to_link(content).html_safe %></strong> <br>
<%= created_at.strftime("%Y-%m-%d %H:%M")%>
</div>
Expand Down

0 comments on commit e4878e4

Please sign in to comment.