Undefined method `each' for nil:NilClass

hi rails developers

@templates_array =get_templates
“#{get_plugin_root}/public/converter/templates/”

def get_templates(templates_path)
dir = Dir.new(templates_path)
templates = Array.new
dir.each do |d|
templates << File.basename(d.gsub(/\/, ‘/’), ‘.xml’) unless
(d=~/^./) #except hidden files
templates.sort!
end
templates
end

in the view page
<%
templateOptions = “”
@templates_array.each do |i|
templateOptions = templateOptions + “#{i}”
end
%>
<%=select_tag “template”, templateOptions %>

I tried to use templates in selection tag but given error like:
undefined method `each’ for nil:NilClass

Quoting Ibrahim D. [email protected]:
[snip]

        @templates_array.each do |i|
            templateOptions = templateOptions + "<option

value=#{i}>#{i}"
end

each does not deal gracefully with emtpy arrays or nil. So change to

if @templates_array
@templates_array.each do |i|
templateOptions = templateOptions + “#{i}”
end
end

or

@templates_array.each do |i|
templateOptions = templateOptions + “#{i}”
end if @templates_array

That last line may need to be

end unless @templates_array.nil?

HTH,
Jeffrey

Jeffrey L. Taylor wrote:

Quoting Ibrahim D. [email protected]:
[snip]

        @templates_array.each do |i|
            templateOptions = templateOptions + "<option

value=#{i}>#{i}"
end

each does not deal gracefully with emtpy arrays or nil. So change to

if @templates_array
@templates_array.each do |i|
templateOptions = templateOptions + “#{i}”
end
end

or

@templates_array.each do |i|
templateOptions = templateOptions + “#{i}”
end if @templates_array

That last line may need to be

end unless @templates_array.nil?

HTH,
Jeffrey

Thank you very much for your help Jeffrey.
I tried this code in another application, it worked. I don t know why
here given this error. there are four files in the folder. the path is
right the codes also look right however that I have error.
I can not add another option and value either.

Ibrahim D. wrote:

Jeffrey L. Taylor wrote:

Quoting Ibrahim D. [email protected]:
[snip]

        @templates_array.each do |i|
            templateOptions = templateOptions + "<option

value=#{i}>#{i}"
end

each does not deal gracefully with emtpy arrays or nil. So change to

if @templates_array
@templates_array.each do |i|
templateOptions = templateOptions + “#{i}”
end
end

or

@templates_array.each do |i|
templateOptions = templateOptions + “#{i}”
end if @templates_array

That last line may need to be

end unless @templates_array.nil?

HTH,
Jeffrey

Thank you very much for your help Jeffrey.
I tried this code in another application, it worked. I don t know why
here given this error. there are four files in the folder. the path is
right the codes also look right however that I have error.
I can not add another option and value either.

OK I found the problem… I defined the @templates_array in the wrong
method.
that s why program could n t get array.

thank you very much Jeffrey
actually you can still give me idea about how to add new option and
value. I still couldnt add it. when I add new option it creates 4 of
them.