开发者

activerecord to_xml update xml version to 1.1

the to_xml to activerecord include the xml declaration as follows.

<?xml version=开发者_JAVA技巧"1.0" encoding="UTF-8"?>

How do we change the version to 1.1 and also change the encoding?

We can use to_xml(:skip_instruct => true) to hide the declaration altogether.


If you are using restfull routes then visiting some_url.xml will give you the results as you have described. If this is the way you are serving your xml then you can define your own xml builder template. It will work in exactly the same way as a view works here is an example

your controller action

  def show
    @obj = SomeClass.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :layout => false }
    end
  end

Then in the views folder where you would normally place the show.html.erb create a show.xml.builder file with the contents looking something like this

xml.someclass do
  xml.id(@obj.id)
  xml.name(@obj.name)
end

In this template you can then add <?xml version="1.1" encoding="UTF-8"?> or whatever xml declarations you wish to add

UPDATE You don't need to be serving views, just a RESTfull route, a controller and an action that has a respond_to that respondes to the XML format. Rails will pick up that you want to render xml and will look for .xml.erb file in the views folder named after the action in exactly the same way as views work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜