How to get the img src value from raw content in haml (Rails )

时间:2017-05-16 09:14:47

标签: ruby-on-rails ruby ruby-on-rails-4 haml

How can i get the img src value from the (= raw post.body) in ruby on rails haml page ? Now i am getting the total contents i.e,

<p>Lorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsum</p>\r\n<p><img style=\"float: right;\" src=\"../../../spree/blog_entry_images/704/large/IMG_4378.jpg?1466600823\" alt=\"\" width=\"404\" height=\"606\" /><img src=\"../../../spree/blog_entry_images/718/large/IMG_4356.jpg?1466608157\" alt=\"\" width=\"405\" height=\"608\" /></p>

I want to show each image in loop. So how can i get this ???

Thanks in Advance.

1 个答案:

答案 0 :(得分:1)

You can use gem nokogiri to extract any html tag, try following code snippet, you might need to require 'nokogiri' while try on console

 content = Nokogiri::HTML(pass_your_html_content)
 srcs    = content.css('img').map{ |i| i['src'] }