Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to get the xml output of a view/controller as string within the same controler

This is the routes file

routes.rb

map.ccda '/ccda/ccda_patient_search', :controller => 'ccda', :action => :ccda_patient_search
map.ccda '/ccda/:id.:format', :controller => 'ccda', :action => :index

ccda_controller.rb

class CcdaController < ApplicationController
def index    
  # 
  # some process 
  # result = User.find(params[:id]).result
  #
  @ccda = result
  respond_to do |format|
    format.xml { render :layout => false, :template=> "index.xml.builder" }
    format.any { redirect_to login_url }
  end   
end

def get_xml
  # render_to_string 'index', :layout=>false, :formats=>[:xml], :locals=>{:id=>147}  => Not working
  # render_to_string '147.xml' => Not working
  #
  # How do I get the output of 'http://localhost/ccda/147.xml' here???
  #
end

end

I will use the url localhost/ccda/147.xml to view/generate the users result as xml

Now I want the output of that url as a string without returning to browser

I've tried to get it from same controller using render_to_string method with diffrent parameters but nothing seems to work

FYI: I am using rails 2.3.12 and Builder::XmlMarkup API

share|improve this question
    
Do you have a xml template view already? –  Mattherick Aug 4 '13 at 0:11
    
@Mattherick yes –  shiva Aug 5 '13 at 3:44

3 Answers 3

How about using the following call (inside controller, have taken the known options from your question):

render_to_string(:template => 'ccda/index.xml.builder', :layout => false, :id => 147)

Due to the documentation, this will work up to Rails version 2.3.8, so I don't know if it is available any more in Rails 2.3.12 you are using.

PS: How about upgrading to at least last version of Rails 3? I don't have a change to test my solution, so it is guesswork more or less.

share|improve this answer
    
it shows Missing template index.erb in view path on trying the code and upgrading will take months since it is a huge project –  shiva Aug 3 '13 at 11:12
    
Changed my answer, but difficult to test (for me). –  mliebelt Aug 3 '13 at 11:36
    
No luck, got Missing template 147.xml in view path –  shiva Aug 3 '13 at 11:47
    
Changed again due to the error message. See guides.rubyonrails.org/v2.3.8/… for the documentation –  mliebelt Aug 3 '13 at 11:59

How about this below?

render_to_string :controller => 'ccda_controller', :action => 'index', :id => 147, :format => :xml
share|improve this answer
up vote 0 down vote accepted

Finally I've found that we need to specify the view file manually because by default rails will be looking for index.erb so what I've done is this

render_to_string( :action=>"index", :view => "/ccda/index.xml.builder",    :format=>:xml,:layout=>false,:id=>146, :template=>"/ccda/index.xml.builder" )

specifying :view and :template manually solved my problem

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.