I need to supply data from my Ruby on Rails database to a jquery grid plugin. At the moment, I'm doing it like this:
@notes = current_user.notes.all
respond_to do |format|
format.xml { render :xml => @notes }
end
But I only want to display certain fields in the grid, so开发者_Go百科 I need to change the code to only send specified fields from the model. What is the easiest way to do this? Thanks for reading.
you can use XML Builder.
Taken from: http://danengle.us/2009/05/generating-custom-xml-for-your-rails-app/
respond_to do |format|
format.html # index.html.erb
format.xml # index.xml.builder
end
# index.xml builder contents
xml.instruct!
xml.posts do
@posts.each do |post|
xml.post do
xml.title post.title
xml.body post.body
xml.published_at post.published_at
xml.comments do
post.comments.each do |comment|
xml.comment do
xml.body comment.body
end
end
end
end
end
end
Another possibility is to override to_xml in your notes model as posted in the comments of the linked site above.
精彩评论