Skip to content

Instantly share code, notes, and snippets.

@bryanl
Created December 27, 2008 17:05
Show Gist options
  • Save bryanl/40279 to your computer and use it in GitHub Desktop.
Save bryanl/40279 to your computer and use it in GitHub Desktop.
In this lesson, we'll learn about RESTful routes in ruby on rails.
The REST support in Rails consists of helper methods and enchanements
to the routing system, designed to impose a particular systle and
order and login on your controlers and, consequently, on the way the
world sees your application. The benefits of Rails' REST support fall
into two categories: Convenience and automatic best practices for you.
A REST interface to your application's services for everyone else.
REST and Rails starts with CRUD or (create -- retrieve -- update --
delete). We'll jump right in to making our application RESTful. First
up, we are going to be undoing the routes we created in previous
lessons.
> config/routes.rb
# remove the old entries search route
# add map.resources :entries
Now, we'll introduce the resources method. The resources method
creates seven routes for you: index, show, new, create, edit, update,
destroy. This rails convention is one that you will use heavily in
your applications. This method makes substantial changes to our
application. We can see these changes by running the routes task from
the command line.
# move the terminal
$ rake routes
When we create resources, rails also creates named routes for us.
entries_path, new_entry_path, and edit_entry path, and entry_path.
I'm sure you are asking yourself where the rest of the named routes
are. The answer is that we don't need them. Rails uses HTTP to infer
which action it should use. The second column of the rake routes output is
the HTTP verb Rails requires to perform that action.
So, what does this mean to us? It means that Rails can decide what
action to use based on the HTTP verb.
In our journal application, we have a path, /entries. When we do a
GET on this path, Rails will use the "index" action. When we do a
POST on this path -- from a web form for instance -- Rails will use the "create"
action.
We can change our new entries form to use our RESTful routes.
# switch back to textmate
> views/entries/new.html.erb
<% form_tag entries_path do %>
# switch back to the web browser
# go to /
# click on the new link
We can still create entries using our form. Now we'll update the link that brings us to this form.
# switch back to textmate
> views/entries/index.html.erb
<%= link_to "new", new_entry_path
We don't have to change the link for showing the entry because it is
the same as the named route we created earlier.
Now, we'll add a way to update our journal entries. For this, we'll
need two more actions: edit and update. Once again, we won't be
checking for errors as we'll be working on this in an upcoming lesson.
> views/entries/show.html.erb
<%= link_to "edit", edit_entry_path(@entry) %>
> controllers/entries_controller.rb
def edit
@entry = Entry.find(params[:id])
end
> views/entries/edit.html.erb
<% form_for @entry do |f| %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %>
<%= f.text_field :body %>
</p>
<p><%= submit_tag "Update entry" %></p>
We'll then add an action for updating our entry
def update
@entry = Entry.find(params[:id])
@entry.update_attributes(params[:entry])
redirect_to @entry_url
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment