不创建 URL link
Do not create URL link
app/vies/items/show.html.erb
<h1><%= @item.name %></h1>
<ul>
<li><%= @item.price %> руб.</li>
<li><%= urls_to_images(@item.description) %></li>
<li>Вес: <%= @item.weight %> кг.</li>
</ul>
app/helpers/application_helper.rb
module ApplicationHelper
def urls_to_images(s)
s.gsub! /\s(http:\/\/.*?)/ , '<img src=""/>'
s.html_safe
end
# def urls_to_links(s)
#
# end
end
浏览器界面:
显示的文字。为什么不输出link?
我认为您在 urls_to_images
方法中犯了错误。试试这个 -
def urls_to_images(s)
s = s.gsub! /\s(http:\/\/.*?)/ , '<img src=""/>'
s.html_safe
end
def urls_to_images(s)
s.gsub! /\s(http:\/\/.*?)(\s|\Z)/ , '<img src=""/>'
s.html_safe
end
**现在可以使用了**
app/vies/items/show.html.erb
<h1><%= @item.name %></h1>
<ul>
<li><%= @item.price %> руб.</li>
<li><%= urls_to_images(@item.description) %></li>
<li>Вес: <%= @item.weight %> кг.</li>
</ul>
app/helpers/application_helper.rb
module ApplicationHelper
def urls_to_images(s)
s.gsub! /\s(http:\/\/.*?)/ , '<img src=""/>'
s.html_safe
end
# def urls_to_links(s)
#
# end
end
浏览器界面:
显示的文字。为什么不输出link?
我认为您在 urls_to_images
方法中犯了错误。试试这个 -
def urls_to_images(s)
s = s.gsub! /\s(http:\/\/.*?)/ , '<img src=""/>'
s.html_safe
end
def urls_to_images(s)
s.gsub! /\s(http:\/\/.*?)(\s|\Z)/ , '<img src=""/>'
s.html_safe
end
**现在可以使用了**