0

我的 XML 错误:

NoMethodError in Admin/xml#index

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

undefined method `name' for "preview":String

Extracted source (around line #1):

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

我的控制器:

def index
    @photographer = Photographer.find(:first)
    #render :layout => false
end 

我的 XML 助手:

   module XmlHelper
        require 'builder'

            def update_xml(photographer, output="preview") 

                xml = Builder::XmlMarkup.new



                 xml.photographer(:name => photographer.name) do    



                  for group in photographer.groups 
                    xml.group(:name => group.name) do 
                    for project in group.projects 
                        xml.project(:name => project.name) do 
                                    for collection in project.collections
                                        xml.collection(:name => collection.name) do 
                                            for image in collection.images
                                                xml.image(image.description, :url => image.image, :id => image.id)


                                            end 
                                        end     
                                    end 
                        end 
                    end
                    end     
                  end       
                end           


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


        end 






end

更新:

使用<% update_xml(photographer, "preview") %>

NameError in Admin/xml#index

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

undefined local variable or method `photographer' for #<#<Class:0x47eb990>:0x47ea238>

新更新使用 <% update_xml(@photographer, "preview") %>

oMethodError in Admin/xml#index

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

undefined method `groups' for #<Photographer:0x45ca2d8>

Extracted source (around line #1):

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

2 回答 2

2

您能否尝试在 Admin/xml#index 中使用“<% update_xml(photographer, "preview") %>" 而不是 <% update_xml("preview") %>,如果您再次遇到错误,请告诉我!

于 2011-09-12T11:56:31.940 回答
0

确保在您的摄影师模型中,您有一个groups关联,例如:

has_many :groups
于 2011-09-12T12:38:32.297 回答