다음 코드가 있습니다.이 도우미를 두 가지 방법으로 분해하고 뷰에서 사용하는 방법은 무엇입니까?
def links_in_body(comment_text)
# show short part of link
regex_url = %r{^(http|https):\/\/|[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?$}
comment_text.gsub!(regex_url) { |link| truncate(link, length: 30) }
# detect links to site's videos and show title
regex = %r{http:\/\/#{request.host_with_port}(\/[\w\/]+)[\.,!\s]?}
comment_text.gsub(regex) do |matched|
params = Rails.application.routes.recognize_path request.path
# if the link we found was a video link, replaced matched string with
# an anchor tag to the video, with the video title as the link text
if params[:controller] == 'videos' && params[:action] == 'show'
video = Video.find params[:id]
link_to "#{video.title} ".html_safe, video_path(video)
# otherwise just return the string without any modifications
else
matched
end
end
end
나는 코드 약어로 https://github.com/vmg/rinku을 사용하기로 결정했다.
이렇게 나왔다.
def links_in_body(comment_text)
# show short part of link
Rinku.auto_link(comment_text) do |url|
truncate(link, length: 30)
end
# detect links to site's videos and show title
regex = %r{http:\/\/#{request.host_with_port}(\/[\w\/]+)[\.,!\s]?}
comment_text.gsub(regex) do |matched|
params = Rails.application.routes.recognize_path request.path
# if the link we found was a video link, replaced matched string with
# an anchor tag to the video, with the video title as the link text
if params[:controller] == 'videos' && params[:action] == 'show'
video = Video.find params[:id]
link_to "#{video.title} ".html_safe, video_path(video)
# otherwise just return the string without any modifications
else
matched
end
end
end
"# 사이트의 동영상에 링크 검색 및 제목 표시"후에 코드 부분이 작동하지 않습니다.
전망 : 나에게 두 가지 방법으로이 도우미를 중단하고보기에 그것을 사용하는 방법을
= links_in_body(comment.body).html_safe
?