Rails 3.1 Replacement for RXML Template

What is the correct way to handle custom XML responses in Rails 3.1?

In Rails 2, I had the following action in the controller:

# GET the blog as a feed def feed @blog = Blog.find(:first, :id => params[:id]]) @blog_id = @blog.id @blog_posts = BlogPost.find(:all, :conditions => ["blog_id = ? AND is_complete = ?", @blog_id, true], :order => "blog_posts.created_at DESC", :limit => 15) render :action => :feed, :layout => false end 

And then the RXML constructor template in the feed.rxml view:

 xml.instruct! :xml, :version=>"1.0" xml.rss(:version=>"2.0") { xml.channel { xml.title(@blog.title) xml.link(url_for(:only_path => false)) xml.description(@blog.subtitle) xml.language('en-us') for blog_post in @blog_posts xml.item do xml.title(blog_post.title || '') xml.link(blog_named_link(blog_post)) xml.description(blog_post.body) xml.tag(blog_post.tag_string) xml.posted_by(blog_post.posted_by.name) end end } } 

Rails 3.1 has excluded the RXML template handler, but I cannot find documentation about what we should replace.

+4
source share
2 answers

First add a respond_to section to your action method

 respond_to do |format| format.xml end 

or just add respond_to :html, :xml controller class definition

Then rename the template file to feed.xml.builder

+7
source

ShiningRay's answer was almost 100% for me, but I also had to force it not to display the default layout to make xml valid.

 # force this to render as xml - this method *only* does xml request.format = "xml" respond_to do |format| format.xml {render :layout => false} end 
+1
source

Source: https://habr.com/ru/post/1382195/


All Articles