You can convert the nil
to string
using .to_s
method
Problem
When you try to call .html_safe
on nil
value it will throw the following error
NoMethodError: undefined method `html_safe' for nil:NilClass
For example
[8] project_rails » html_content = nil
=> nil
[9] project_rails » html_content.html_safe
NoMethodError: undefined method `html_safe' for nil:NilClass
from (pry):9:in `__pry__'
Solution
You can convert the nil
to string using .to_s
For example
[10] project_rails » html_content = nil
=> nil
[11] project_rails » html_content.to_s.html_safe
=> ""
[12] project_rails »
So your code should be like this
def render_flash_messages
s = ''
flash.each do |k,v|
s << content_tag('div',v.to_s.html_safe, :class => "flash #{k}", :id => "flash_#{k}")
end
s.html_safe
end