如何下载使用生成器生成的xml文件并另存为Rails中的xml文件

时间:2019-01-09 19:38:58

标签: ruby-on-rails ruby xml nokogiri

我已经尝试在索引控制器中使用以下代码

    def index
      @forms = Form.all
      data = render_to_string( :action => :index )
      send_data data, :filename => "xyz.xml", :disposition => 'attachment'
    end 

,但是它保存对象ID,但不保存带有值的xml输出。

我想保存在index.xml.builder中可以在视图中看到的输出

这是index.xml.builder视图


    xml.instruct! :xml, :version => "1.0", :encoding => 'UTF-8'

    @forms.each do |form|
      xml.admin do
        xml.applicant_info do
          xml.id form.form_id
          xml.company_name form.company_name
          xml.submission_description form.submission_description

      form.applicants.each do |applicant|
            xml.applicant_contacts do
              xml.applicant_contact do
                xml.applicant_contact_name applicant.applicant_contact_name, :'applicant-contact-type' => applicant.applicant_contact_type 
                xml.telephones do
                  xml.telephone applicant.telephone, :'telephone-number-type' => applicant.telephone_number_type
                end 
                xml.emails do
                  xml.email applicant.email
                end 
              end
            end
          end
        end
        xml.application_set do
          xml.application_containing_files form.application_containing_files
          xml.application_information do
            xml.application_number form.application_number, :'application-type' => form.application_type 
          end
        end
      end
    end
  end
end

1 个答案:

答案 0 :(得分:0)

Rather than writing xml builder in your action view can you write that code in a private method in your action.

def index
  @forms = Form.all
  send_data build_xml.to_xml, :filename => "xyz.xml", :disposition => 'attachment'
end 

private

def build_xml
  Nokogiri::XML::Builder.new do |xml|
    xml.root do
      @forms.each do |form|
        xml.admin do
          xml.applicant_info do
            xml.id form.form_id
            xml.company_name form.company_name
            xml.submission_description form.submission_description

        form.applicants.each do |applicant|
              xml.applicant_contacts do
                xml.applicant_contact do
                  xml.applicant_contact_name applicant.applicant_contact_name, :'applicant-contact-type' => applicant.applicant_contact_type 
                  xml.telephones do
                    xml.telephone applicant.telephone, :'telephone-number-type' => applicant.telephone_number_type
                  end 
                  xml.emails do
                    xml.email applicant.email
                  end 
                end
              end
            end
          end
          xml.application_set do
            xml.application_containing_files form.application_containing_files
            xml.application_information do
              xml.application_number form.application_number, :'application-type' => form.application_type 
            end
          end
        end
      end
    end
  end
end