Ruby on Rails Thursday, September 30, 2010

Newb Newb wrote:
> Actually I would like to know how to send the picture name in the xml
> response
>
> fox example picture = Picture.find(params[:id])
>
> Here i need to send the picture.id in the xml response...

I'm not sure what you're really asking, but maybe this will help:

Terminal
--------------
rails g scaffold picture name:string mime_type:string url:string
rake db:migrate
rails s (or ./script/server for Rails < 3.0)

THE CODE...

picture.rb
---------------
class Picture < ActiveRecord::Base
end

pictures_controller.rb
------------------
# GET /pictures/1
# GET /pictures/1.xml
def show
@picture = Picture.find(params[:id])

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

Terminal (again)
------------------------
$ curl http://localhost:3000/pictures/1.xml
<?xml version="1.0" encoding="UTF-8"?>
<picture>
<created-at type="datetime">2010-09-30T14:14:32Z</created-at>
<id type="integer">1</id>
<mime-type>image/jpeg</mime-type>
<name>Fox Example Picture</name>
<updated-at type="datetime">2010-09-30T14:14:32Z</updated-at>
<url>http://images.nationalgeographic.com/wpf/media-live/photos/000/006/cache/red-fox_679_600x450.jpg</url>
</picture>
--
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@googlegroups.com.
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.

No comments:

Post a Comment