2011-02-09 2 views

답변

18

레일에는 auto_link 텍스트 도우미가 있습니다.

auto_link("Go to http://www.rubyonrails.org and say hello to [email protected]") 
# => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and 
#  say hello to <a href=\"mailto:[email protected]\">[email protected]</a>" 

auto_link("Visit http://www.loudthinking.com/ or e-mail [email protected]", :link => :urls) 
# => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a> 
#  or e-mail [email protected]" 
+0

나는 그것에 대해 들어 본 적이 없어요. 큰! – fl00r

+2

Rails 3.1 이상을 사용중인 경우 제공된 링크에서 nikstep – Dean

+0

auto_link를 찾을 수 없음에 따라 rails_autolink를 사용하십시오. 대체 문서 : http://apidock.com/rails/ActionView/Helpers/TextHelper/auto_link –

-3

먼저

 
IPv4_PART = /\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]/ # 0-255 
REGEXP = %r{ 
    https?://             # http:// or https:// 
    ([^\s:@]+:[^\s:@]*@)?          # optional username:[email protected] 
    ((([^\W_]+\.)*xn--)?[^\W_]+([-.][^\W_]+)*\.[a-z]{2,6}\.? | # domain (including Punycode/IDN)... 
     #{IPv4_PART}(\.#{IPv4_PART}){3})      # or IPv4 
    (:\d{1,5})?             # optional port 
    ([/?]\S*)?             
}iux 

다음 예를 들어, 정규 표현식 매칭 HTTP 문자열을 정의해야합니다, 주석 몸이 STR 가정하자, 당신이 :

 
str.gsub(REGEXP) do |m| 
    link_to m, m 
end 
+1

자신의 URL의 정규 롤링하는 것은 정말 끔찍한 생각입니다 - 당신은 ** ** 실수를합니다. 라이브러리를 사용하십시오. :) –

0

또한 "auto_html을 사용할 수 있습니다 "보석, https://github.com/dejan/auto_html 참조.

면책 조항 : 아직 직접 사용하지 않았지만 원하는대로 처리 할 수있는 것 같습니다.

+0

재미있는 질문이 무엇을 요구했지만 무엇보다 새로운 보석이 필요합니다. auto_link가 더 간단합니다. –

0

의견을 말씀하면 Markdown과 같은 생각을하는 것이 좋습니다. 그런 다음 Markdown 엔진이 이러한 문제에 대해 걱정할 수 있습니다. 에서

관련 문제