Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails XML错误未定义nil:NilClass的方法“name”_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rails XML错误未定义nil:NilClass的方法“name”

Ruby on rails Rails XML错误未定义nil:NilClass的方法“name”,ruby-on-rails,ruby,ruby-on-rails-3,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:

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>
我的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     

在编写kids之前,请尝试设置n=0

在写kidsn之前尝试设置n=0参见代码顶部问题是n被设置为实数并影响kidsmovies在那里我创建了k=0并替换了kidsmovies中的
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     
 xmlmenu.item(:name=> 'whatever'},  :type=>"project", :id=> kidsmovies[n].id)