Hello, I noticed that with the latest version of Rails, scaffolding in the controller already has resourceful methods. Based on the comments in the code, it seems that all of this is built in, but I''m having some trouble with creating new records by posting xml data. I have a SubversionRepository model and SubversionRepositories controller. When I access /subversion_repositories.xml I get a list of repositories in a format like: <?xml version="1.0" encoding="UTF-8"?> <subversion-repositories type="array"> <subversion-repository> ... <name>repo1</name> <group>group1</group> </subversion-repository> <subversion-repository> ... </subversion-repository> </subversion-repositories> Based on this format, I tried posting a new repository to the address /subversion_repositories.xml using the following xml. <subversion-repository> <name>newrepo</name> <group>newgroup</group> </subversion-repository> I store this in the file repo.xml and post it using the following code. require ''net/http'' require ''uri'' require ''rexml/document'' file = File.new "repo.xml" xml_document = REXML::Document.new(file) puts "sending XML in http POST" http_client = Net::HTTP.new(''localhost'', 3000) http_result = http_client.post(''/subversion_repositories.xml'', xml_document.to_s) puts "result type: " + http_result.class.name puts "result: " + http_result.body.to_s The script returns the following output. sending XML in http POST result type: Net::HTTPClientError result: <?xml version="1.0" encoding="UTF-8"?> <errors> <error>Name can''t be blank</error> <error>Group can''t be blank</error> </errors> The log file has an entry similar to the following. Processing SubversionRepositoriesController#create (for XXXXX at 2008-03-13 14:54:00) [POST] Session ID: XXXXX Parameters: {"format"=>"xml", "action"=>"create", "controller"=>"subversion_repositories", "<subversion-repository>\n <name>newrepo</name>\n <group>newgroup</group>\n <created-at type"=>"''datetime'' nil=''true''/>\n <updated-at type=''datetime''>2008-03-12T16:15:51-05:00</updated-at>\n </subversion-repository>"} Completed in 0.01600 (62 reqs/sec) | Rendering: 0.00000 (0%) | DB: 0.00000 (0%) | 422 Unprocessable Entity [http://localhost/subversion_repositories.xml] Does anyone know what I''m doing wrong? Does this functionality not come built in? Thanks for your help. Justin -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Mar-13 22:36 UTC
Re: Posting xml to create new record
Have you tried headers = {''Content-Type'' => ''text/xml''} http_result = http_client.post(''/subversion_repositories.xml'', xml_document.to_s,headers) On Mar 13, 1:10 pm, Justin Johnson <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > > I noticed that with the latest version of Rails, scaffolding in the > controller already has resourceful methods. Based on the comments in > the code, it seems that all of this is built in, but I''m having some > trouble with creating new records by posting xml data. > > I have a SubversionRepository model and SubversionRepositories > controller. When I access /subversion_repositories.xml I get a list of > repositories in a format like: > > <?xml version="1.0" encoding="UTF-8"?> > <subversion-repositories type="array"> > <subversion-repository> > ... > <name>repo1</name> > <group>group1</group> > </subversion-repository> > <subversion-repository> > ... > </subversion-repository> > </subversion-repositories> > > Based on this format, I tried posting a new repository to the address > /subversion_repositories.xml using the following xml. > > <subversion-repository> > <name>newrepo</name> > <group>newgroup</group> > </subversion-repository> > > I store this in the file repo.xml and post it using the following code. > > require ''net/http'' > require ''uri'' > require ''rexml/document'' > > file = File.new "repo.xml" > xml_document = REXML::Document.new(file) > > puts "sending XML in http POST" > http_client = Net::HTTP.new(''localhost'', 3000) > http_result = http_client.post(''/subversion_repositories.xml'', > xml_document.to_s) > puts "result type: " + http_result.class.name > puts "result: " + http_result.body.to_s > > The script returns the following output. > > sending XML in http POST > result type: Net::HTTPClientError > result: <?xml version="1.0" encoding="UTF-8"?> > <errors> > <error>Name can''t be blank</error> > <error>Group can''t be blank</error> > </errors> > > The log file has an entry similar to the following. > > Processing SubversionRepositoriesController#create (for XXXXX at > 2008-03-13 14:54:00) [POST] > Session ID: XXXXX > Parameters: {"format"=>"xml", "action"=>"create", > "controller"=>"subversion_repositories", "<subversion-repository>\n > <name>newrepo</name>\n <group>newgroup</group>\n <created-at > type"=>"''datetime'' nil=''true''/>\n <updated-at > type=''datetime''>2008-03-12T16:15:51-05:00</updated-at>\n > </subversion-repository>"} > Completed in 0.01600 (62 reqs/sec) | Rendering: 0.00000 (0%) | DB: > 0.00000 (0%) | 422 Unprocessable Entity > [http://localhost/subversion_repositories.xml] > > Does anyone know what I''m doing wrong? Does this functionality not come > built in? > > Thanks for your help. > Justin > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Have you tried > headers = {''Content-Type'' => ''text/xml''} > http_result = http_client.post(''/subversion_repositories.xml'', > xml_document.to_s,headers) >That is exactly what I needed. Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---