I have a situation where I receive HTML encoded information in our database column in some instance, in some other instances I receive regular text.
I have this code:
my_string = Nokogiri::HTML.parse(my_string).text
This works if my_string
is HTML-encoded but does not work if it's regular text.
Is there a way I can perform the following check?
if html_encoded, Nokigiri::HTML.parse else just return my_string as is.
I am beginning to think Rails is handling this weirdly. Here is my model code:
def show_name
name = Nokogiri::HTML.parse(name).text
name
end
Here is my view code:
<tbody>
<% names.each do |t| %>
<tr class="<%= return_cd_error?(t.show_return_cd) ? 'error' : '' %>">
<td><%= t.name %></td>
</tr>
<% end %>
If I use binding.pry
before the name, the name "John Doe" is returned "" before and after the parse, which is strange:
[2] pry(#<Test::Sess>)> name
=> "Hugh Geissler"
[3] pry(#<Test::Sess>)> name = Nokogiri::HTML.parse(name).text
=> ""
However, if I remove the Nokogiri parse code, it displays fine.