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.
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.
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:
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..
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.