1

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 The error message

thank you for your answers.

EDIT:

this is the application_helper

the application_helper

Paul
  • 25,812
  • 38
  • 124
  • 247
aganthie
  • 33
  • 6
  • What is in line 306 of application helper? Can you post that method – j-dexx Feb 12 '16 at 15:11
  • Could you write text in these images down to your question? This will keep StackOverflow consistent and transparent for search engines. – Paul Feb 12 '16 at 15:46

3 Answers3

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
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
spickermann
  • 100,941
  • 9
  • 101
  • 131
0

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.

jkraemer
  • 357
  • 1
  • 6