Ruby on Rails - how to display as xml models with has_many associations

I have a ruby ​​on rails application that has two models - sheets and sub_tests. Ltest has an association ' has_many' with sub_tests. The show method for ltests has the following.

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

This creates an ltest object. However, sub_tests owned by ltest are not displayed.

How can i do this?

<ltest>
....
   <sub_test>
   ...
   </sub_test>
   <sub_test>
   ...
   </sub_test>
</ltest>

I tried to do this with the following look:

But this generates two XML documents, not one with sub_tests built into the libraries.

Any suggestions on how I can do this?

+3
source share
5 answers

erb. , ( , rails 2.3.4) :

format.xml  { render :xml => @ltest.to_xml(:include => :sub_tests) }
+13

, . , .

<%= @ltest.to_xml(:include => {
        :test_group => { :include => [ :user ]},
        :sub_tests => { :include => {
            :attachments => {},
            :errors => {},
            :test_bugs => {},
        } },
        :attachments => {},
        :errors => {},
        :test_bugs => {},
        :test_nodes => { :include => {
            :node => { :include => [ :networks ]},
            :attachments => {},
        }}
    } ) %>
+5

:

render :xml => @record.to_xml(:include => {:groups => {:include => {:questions => {:include => :items}}}})
+3

show.xml.erb .

<%= @ltest.to_xml :include => [ :sub_tests ]%>

, sub_tests (test_logs, errors), .

+1

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


All Articles