简体   繁体   中英

Ruby-on-Rails: Help with render: layout => false

I am trying to access a rails app resource from an API (it sends an Application/XML GET request) and I would like to not have to parse the XML file.

In my resources controller I have the following:

def get_resource
    @my_resource = Resources.new
    render :xml => @my_resource
end

which produces an xml file as expected. If i replace it with:

render :layout => false

my API reports a "template missing" error. I've also tried the following:

render :xml => @identity, :layout => false

But the page renders anyway. What's the right way to go about this?

When you render :xml, it does not use a layout because it doesn't use any template either. By specifying :layout => false, you tell rails to look for a template which does not exist.

Now, if you don't want to parse an xml file, then you have a few alternatives. Either:

render :json => @my_resource

or

render :text => "My resource name is: #{@my_resource.name}" # Whatever you want

It all depends on how you want the result to look, what your API expects to receive. So if you don't find any of this helpful, give an example of how you want the response to look.

def get_resource
  @my_resource = Resources.new
  respond_to do |wants|
    wants.xml { render :xml => @my_resource }
    wants.html { render :layout => false }
  end
end

Read this article: http://tokumine.wordpress.com/2009/09/13/how-does-respond_to-work-in-the-rails-controllers/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM