问题
I am a beginer in ruby and I want to write a plugin for redmine. I have written a plugin which was running since I use 'flash[:var]' in a controller of my plugin. Now when I want to access to all my pages I have an error message that I not understand.
Ruby version : ruby 1.9.3p484
Rails version : rails 3.2.19
this is the error message
thank you for your answers.
EDIT:
this is the application_helper
回答1:
It seems like you ran into situations in which you have nil
values in your flash
. Imagine you have a flash
like { error: nil }
, then you would call v.html_safe
in the content_tag
- what will cause the error.
You might want to extract all values from the flash
that are present?
, before calling content_tag
:
def render_flash_messages
flash.select { |k, v| v.present? }.map do |type, text|
content_tag(:div, text.html_safe, class: "flash #{type}", id: "flash_#{typ}")
end.join
end
回答2:
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
回答3:
The problem is that you put nil
into the flash which is something Redmine's render_flash_messages
method is not designed to handle.
Changing that method to deal with nil values will work but is not something you should do from a Redmine plugin. Instead, find out where and why in your plugin you put nil
into the flash and simply don't do it.
来源:https://stackoverflow.com/questions/35365927/actionviewtemplateerrorundefined-method-html-safe-for-nilnilclass