CuriousNewbie
2010-Oct-27 05:23 UTC
Rails 3 - Creating a JSON response to display Search Results
Hello, I''m working to have Rails 3 respond with a JSON request which
will then let the app output the search results with the jQuery
template plugin...
For the plugin to work, it needs this type of structure:
[
{ title: "The Red Violin", url: "/adadad/123/ads",
desc: "blah
yada" },
{ title: "Eyes Wide Shut", url: "/adadad/123/ads",
desc: "blah
yada" },
{ title: "The Inheritance", url: "/adadad/123/ads",
desc: "blah
yada" }
]
In my Rails 3 controller, I''m getting the search results which come
back as @searchresults, which contain either 0 , 1 , or more objects
from the class searched.
My question is how to convert that to the above structure (JSON)...
Thank you!
... The front-end search page will need to work for multiple models
which have different db columns. That''s why I''d like to learn
how to
convert that to the above to normalize the results, and send back to
the user.
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Peter De Berdt
2010-Oct-27 07:56 UTC
Re: Rails 3 - Creating a JSON response to display Search Results
On 27 Oct 2010, at 07:23, CuriousNewbie wrote:> Hello, I''m working to have Rails 3 respond with a JSON request which > will then let the app output the search results with the jQuery > template plugin... > > For the plugin to work, it needs this type of structure: > > [ > { title: "The Red Violin", url: "/adadad/123/ads", desc: "blah > yada" }, > { title: "Eyes Wide Shut", url: "/adadad/123/ads", desc: "blah > yada" }, > { title: "The Inheritance", url: "/adadad/123/ads", desc: "blah > yada" } > ] > > In my Rails 3 controller, I''m getting the search results which come > back as @searchresults, which contain either 0 , 1 , or more objects > from the class searched. > > My question is how to convert that to the above structure (JSON)... > > Thank you! > > ... The front-end search page will need to work for multiple models > which have different db columns. That''s why I''d like to learn how to > convert that to the above to normalize the results, and send back to > the user.Something like this should work: http://gist.github.com/648637 We are using a similar concept in our main application, but much more complex (generating tree structures out of complex node relationships). This is a basic implementation, but you can easily make it do more and yet all functionality is nicely encapsulated. Hope this helps. Best regards Peter De Berdt -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.