Rails XML 未定义方法“名称”对于“预览”:字符串
我的 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(摄影师,“预览”)%&gt;
:
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,“预览”)%>
:
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>
My XML error:
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>
My controller:
def index
@photographer = Photographer.find(:first)
#render :layout => false
end
My XML helper:
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:
Using <% 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>
NEW UPDATE Using <% 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用“<% update_xml(photographer, "preview") %>”而不是 <% update_xml("preview") %>在 Admin/xml#index 中,如果再次出现错误,请告诉我!
Could you please try using "<% update_xml(photographer, "preview") %>" instead of <% update_xml("preview") %> in Admin/xml#index, let me know if you get error again!
确保在您的 Photographer 模型中具有
groups
关联,例如:Make sure in your Photographer model you have a
groups
association such as: