Rails XML错误未定义的方法`name'代表nil:NilClass

时间:2011-10-14 10:26:53

标签: ruby-on-rails ruby ruby-on-rails-3

A有一个提供错误的XML帮助程序:

NoMethodError in Admin/xml#index

Showing C:/Rails/kimnew/app/views/admin/xml/index.rhtml where line #1 raised:

undefined method `name' for nil:NilClass

Extracted source (around line #1):

1: <% update_xml("preview") %>
2: 
3: 
4: <h2>Preview/publish</h2>

我找到了它是孩子们的名字:

xmlmenu.item(:name=> "#{convert_html_entities(kidsmovies[n].name)}",  :type=>"project", :id=> kidsmovies[n].id) 

我的XML助手:

def update_xml( output )
xmlmenu = Builder::XmlMarkup.new 

            l = Movielimit.new
            l.reel_limit = Photographer.where(:reel_online => true).count
            l.kids_limit = Photographer.where(:kids_online => true).count
            n = 0
    xmlcdata = Builder::XmlMarkup.new

        movies = Photographer.find(:all)
        #loop through all movies for reel..
        reelmovies = Photographer.where(:reel_online => true).order("reel_order")
        #loop through all movies for kids..
        kidsmovies = Photographer.find(:all, :order => 'kids_order ASC', :conditions => ["kids_online=?", true]) 

#write reel         
        xmlmenu.item(:name=>"reel",  :type=>"menuItem", :id=>1)     do  
            while n < movies.length && n < l.reel_limit
                xmlmenu.item(:name=> convert_html_entities(reelmovies[n].name),  :type=>"project", :id=> reelmovies[n].id)                                      
                n = n + 1
            end 
        end

        #write kids     
            xmlmenu.item(:name=>"kids",  :type=>"menuItem", :id=>9)     do  
                while n < (kidsmovies.length + 2)
                    xmlmenu.item(:name=> "#{convert_html_entities(kidsmovies[n].name)}",  :type=>"project", :id=> kidsmovies[n].id)                                     
                    n = n + 1
                end 
            end

#hardwiret textpage for the menu
        about = Textpage.find(1)
        if about.text && about.text.size >1  
            xmlmenu.item(:name=>"About", :type=>"textpage", :id=>3)
        end     

        ###2.content

            #textpages
        textpages = Textpage.find(:all)
        xmltextpages = Builder::XmlMarkup.new           
        for t in textpages

            #special hardwired case for contactpage
            if t.id ==  2 
                xmltextpages.textpage(:name => t.name, :id=> "4", :behaviour=>"permanent") do 
                    xmltextpages.text do    
                        xmltextpages.cdata!(t.text)
                     end    
                end

            #special hardwired case for aboutpage   
            elsif t.id == 1     
                if t.text && t.text.size >1  
                    xmltextpages.textpage(:name => t.name, :id=> "3") do 
                        xmltextpages.text do    
                            xmltextpages.cdata!(t.text)
                         end    
                     end
                end                                                             
            end 

        end 

        #projects/movies            
        xmlmovies = Builder::XmlMarkup.new
        for m in movies 
            xmlmovies.project(:id=> m.id, :name=> convert_html_entities(m.name)) do 
                if m.image
                    xmlmovies.img(:id=>m.id, :url=> m['image_file_name'])
                end                         
                if m.flv
                    xmlmovies.movie(:id=> m.id, :url=>m['flv_file_name'])
                end                                                                                 
                if m.quicktime
                    #xmlmovies.download(:id=>m.id, :url => m['quicktime'])
                    xmlmovies.download(:id=>m.id, :url => "photographer/quicktime/#{m.id}/#{m['quicktime_file_name']}") 
                end                 
                xmlmovies.text(m.text)                                      
            end         
        end 




content = "<?xml version='1.0' encoding='UTF-8'?>      
             <site>
               <general name='general' type='general'><imagePath>photographer/image/</imagePath><moviePath>../photographer/flv/</moviePath></general>           
               #{xmlmenu.to_xml}
               #{xmlmovies.to_xml}                     
               #{xmltextpages.to_xml}
               </site>"

            File.open("#{RAILS_ROOT}/public/xml/#{output}.xml", "w") do |f|

            f.puts ("#{content}")   

            end
# AWS::S3::S3Object.store(file, content, "bucket", :access => :public_read)

    end     

另外一个问题是,当有6个摄影师行kids_online = true时,只有4个项目项目会生成儿童菜单项目。我将名称设置为以下内容时传递错误:

 xmlmenu.item(:name=> 'whatever'},  :type=>"project", :id=> kidsmovies[n].id)   

1 个答案:

答案 0 :(得分:0)

在写小孩之前尝试设置n=0