New to Rails and REST here. Say I have a simple Rails app called Todos. I generate a controller for Todos and so via REST I can obtain Todo at index 1 by calling say, localhost:3000/todos/1 I can also obtain all todos by doing localhost:3000/todos so how can I use REST by querying for a todo that has a specific title? I''m not sure how to do this. regards, Michael -- 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.
On 5 April 2011 16:14, Michael Hanna <taomailings-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> so how can I use REST by querying for a todo that has a specific > title? I''m not sure how to do this.So you''re still interested in a list of todos, but this time filtered depending on the title. You can have the index action receive a search string via params. And to keep your controller lean, pass this on to a class method or even better, a named_scope in your Todo model which will return filtered results. Regards, Franz -- 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.
Franz Strebel wrote in post #991045:> On 5 April 2011 16:14, Michael Hanna <taomailings-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> so how can I use REST by querying for a todo that has a specific >> title? I''m not sure how to do this. > > So you''re still interested in a list of todos, but this time filtered > depending > on the title. > > You can have the index action receive a search string via params. And > to keep your controller lean, pass this on to a class method or even > better, > a named_scope in your Todo model which will return filtered results.This is a good approach for relatively basic search requirements, which in a lot of cases is all one needs. In cases where more complex search is required it is also possible to create a new resource used to perform search. Say, for example, you wanted to be able to save searches for later use, or provide an advanced search form. I think it''s important to keep in mind, even though this might seem like a more advanced approach for someone new to Ruby on Rails and REST. It is also worth taking the time to read though Fielding''s Dissertation: http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm Especially these sections: 5.2.1.1 Resources and Resource Identifiers 5.2.1.2 Representations -- 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-/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.
Do you know of a quick and dirty example? I understand the concept you''re describing, but I don''t know where I can find an example of the syntax in the Rails controller to get it right. Also, I''m interested in possibly making the REST call from an abstracted view/controller framework like SproutCore, not necessarily from a Form.. regards, Michael On Apr 5, 10:28 am, Franz Strebel <franz.stre...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5 April 2011 16:14, Michael Hanna <taomaili...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > so how can I use REST by querying for a todo that has a specific > > title? I''m not sure how to do this. > > So you''re still interested in a list of todos, but this time filtered depending > on the title. > > You can have the index action receive a search string via params. And > to keep your controller lean, pass this on to a class method or even better, > a named_scope in your Todo model which will return filtered results. > > Regards, > Franz-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 5 April 2011 19:27, Michael Hanna <taomailings-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Do you know of a quick and dirty example? I understand the concept > you''re describing, but I don''t know where I can find an example of theOK, in your Todo model, create a named scope named_scope :search, lambda { |s| options = { :order => :title } options[:conditions] = [ "upper(title) like :search_string", { :search_string => "%#{s.strip.upcase}%" } ] unless s.blank? options } Then in your controller''s index action def index @todos = Todo.search(params[:search]) end Since params is a Hash, it will return nil if it does not contain a key called :search, so the named_scope won''t set any conditions and just return your Todos sorted alphabetically by title. You can quickly test if it works by appending a search param to the URL, eg http://localhost:3000/todos?search=code BTW, my code may have errors as I just quickly wrote that up without proper testing. I hope this gives you a good idea of how it works. Regards, Franz -- 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.