Rails XML 生成器未渲染
Rails XML builder not rendering
我在 Rails 4 应用程序的请求响应正文中呈现 XML 时遇到问题。在下面的示例中,响应正文是空白的。我在模板中放置了一个调试器,所以我知道它会运行它但不会渲染任何东西。
我创建了一个简单的 rails 应用程序来演示我在使用构建器 return xml 时遇到的问题。任何人都可以指出这个例子的(可能是愚蠢的简单)问题吗?
控制器、模板和测试如下:
controllers/bars_controller.rb
require 'builder'
class BarsController < ApplicationController
before_action :set_bar, only: [:show]
# GET /bars/1
# GET /bars/1.json
def show
@xml = Builder::XmlMarkup.new
render template: 'bars/show.xml.builder', formats: [:xml]
end
private
# Use callbacks to share common setup or constraints between actions.
def set_bar
@bar = Bar.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def bar_params
params.require(:bar).permit(:foo, :bar)
end
end
/views/bars/show.xml.builder
@xml.instruct!
@xml.bar do
@xml.foo(@bar.foo)
@xml.bar(@bar.bar)
end
/测试/controllers/bars_controller_test.rb
require 'test_helper'
class BarsControllerTest < ActionController::TestCase
setup do
@bar = bars(:one)
end
test "should show bar" do
get :show, id: @bar
assert_response :success
assert_match "<bar>", response.body
end
end
调试会话
1: @xml.instruct!
2: binding.pry
=> 3: @xml.bar do
4: @xml.foo(@bar.foo)
5: @xml.bar(@bar.bar)
6: end
[2] pry(#<#<Class:0x007fc669e9f610>>)> @xml.bar do
[2] pry(#<#<Class:0x007fc669e9f610>>)* @xml.foo(@bar.foo)
[2] pry(#<#<Class:0x007fc669e9f610>>)* @xml.bar(@bar.bar)
[2] pry(#<#<Class:0x007fc669e9f610>>)* end
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?><bar><foo>MyString</foo><bar>MyString</bar></bar>"
您的问题似乎是创建 Builder::XmlMarkup.new
的实例。删除构建器的显式创建,使您的控制器看起来像这样:
def show
# You can also simplify by removing "bars/"
render 'bars/show.xml.builder', formats: [:xml]
end
您的视图应如下所示:
xml.instruct!
xml.bar do
xml.foo(@bar.foo)
xml.bar(@bar.bar)
end
我在 Rails 4 应用程序的请求响应正文中呈现 XML 时遇到问题。在下面的示例中,响应正文是空白的。我在模板中放置了一个调试器,所以我知道它会运行它但不会渲染任何东西。
我创建了一个简单的 rails 应用程序来演示我在使用构建器 return xml 时遇到的问题。任何人都可以指出这个例子的(可能是愚蠢的简单)问题吗?
控制器、模板和测试如下:
controllers/bars_controller.rb
require 'builder'
class BarsController < ApplicationController
before_action :set_bar, only: [:show]
# GET /bars/1
# GET /bars/1.json
def show
@xml = Builder::XmlMarkup.new
render template: 'bars/show.xml.builder', formats: [:xml]
end
private
# Use callbacks to share common setup or constraints between actions.
def set_bar
@bar = Bar.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def bar_params
params.require(:bar).permit(:foo, :bar)
end
end
/views/bars/show.xml.builder
@xml.instruct!
@xml.bar do
@xml.foo(@bar.foo)
@xml.bar(@bar.bar)
end
/测试/controllers/bars_controller_test.rb
require 'test_helper'
class BarsControllerTest < ActionController::TestCase
setup do
@bar = bars(:one)
end
test "should show bar" do
get :show, id: @bar
assert_response :success
assert_match "<bar>", response.body
end
end
调试会话
1: @xml.instruct!
2: binding.pry
=> 3: @xml.bar do
4: @xml.foo(@bar.foo)
5: @xml.bar(@bar.bar)
6: end
[2] pry(#<#<Class:0x007fc669e9f610>>)> @xml.bar do
[2] pry(#<#<Class:0x007fc669e9f610>>)* @xml.foo(@bar.foo)
[2] pry(#<#<Class:0x007fc669e9f610>>)* @xml.bar(@bar.bar)
[2] pry(#<#<Class:0x007fc669e9f610>>)* end
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?><bar><foo>MyString</foo><bar>MyString</bar></bar>"
您的问题似乎是创建 Builder::XmlMarkup.new
的实例。删除构建器的显式创建,使您的控制器看起来像这样:
def show
# You can also simplify by removing "bars/"
render 'bars/show.xml.builder', formats: [:xml]
end
您的视图应如下所示:
xml.instruct!
xml.bar do
xml.foo(@bar.foo)
xml.bar(@bar.bar)
end