Forum: Ruby on Rails restful views

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nicholas W. (Guest)
on 2008-10-09 22:59
(Received via mailing list)
Hi *,
  I'm writing a Restful service that should map a pre-existing
partially restful site.
What I find confusing is how to display models in my views. I'm
writing all XML in builder using the respond_to API, but everything I
found on the net uses the to_xml method of model's instances, and
that's not my case because I don't want to display every attribute of
my models.
Problem is that to_xml for example gives type informations for most
(not all) attributes.
Is there a place where this is documented ? How should I write my
builder views ? Try a to_xml and rewrite ? Ouch ... :p

TIA, much appreciated,
  ngw
Youssef Chaker (Guest)
on 2008-10-11 03:42
(Received via mailing list)
Hi,
In your controller you can specify what you want to be included in the
xml and filter out the stuff you don't want to be there. For example:

render :xml => @survey_response.to_xml( :dasherize => false,
            :include => {
              :user =>
                {:include =>
                  {:city =>
                    {:include =>
                      {:state => {}
                      }
                    }
                  }
                },
              :answers =>
                {:include =>
                  {:question => {}, :choice => {}
                  }
                }
            }
          )

that's part of controller i have for one of my apps where i feed back
to the xml the answers given by a user who filled a form. And i choose
to include the city/state information with the answers, whereas if i
do the following:

render :xml => @survey_response.to_xml( :dasherize => false,
            :include => {
              :user =>
                {:include =>
                  {:city =>
                    {:include =>
                      {:state => {}
                      }
                    }
                  }
                }
            }
          )

I would be just including the city/state info and leaving out the
answers. You could do something similar for your model.

I hope this is helpful

On Oct 9, 2:58 pm, Nicholas W. <removed_email_address@domain.invalid>
This topic is locked and can not be replied to.