Query REST

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

On 5 April 2011 16:14, Michael H. [email protected] 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

Franz S. wrote in post #991045:

On 5 April 2011 16:14, Michael H. [email protected] 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

On 5 April 2011 19:27, Michael H. [email protected] 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 the

OK, 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs