About this entry




RESTful Controllers in Ruby on Rails

The following article describes how to create the basic operations of create, read, update, and destroy on a simple table using RESTful resource scaffolding.

Liked it? !

The following posts are based on a Ruby on Rails workshop I am teaching in Thimphu, Bhutan. It is not a complete tutorial since it does not include explanations. It may in the future but I do not know when I will have time to add them, so I am publishing it now as is to make it available to others that may find it useful. The sketches describe how to progressively build an Image Management System from the ground-up.

Before reading this article it is recommended that you read this first: Hello World! An Introduction to Controllers, Views, and Layouts.

Index

  1. Creating the MySQL Database
  2. Creating the Scaffold Controller
  3. Creating the Table
  4. Testing the Output
  5. Changing the Generated Code

 

Creating the MySQL Database

To create a database, type into the command-line:

mysqladmin -u root create ims_development

Creating the Scaffold Controller

To create the scaffold controller, type into the command-line:

ruby script/generate scaffold_resource country title:string

Output:

exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/countries exists test/functional/ exists test/unit/ create app/views/countries/index.rhtml create app/views/countries/show.rhtml create app/views/countries/new.rhtml create app/views/countries/edit.rhtml create app/views/layouts/countries.rhtml create public/stylesheets/scaffold.css create app/models/country.rb create app/controllers/countries_controller.rb create test/functional/countries_controller_test.rb create app/helpers/countries_helper.rb create test/unit/country_test.rb create test/fixtures/countries.yml create db/migrate create db/migrate/001_create_countries.rb route map.resources :countries

Creating the Table

Restrict the size of the title to 100 and make MyISAM the default database engine for the creation of the table countries by updating db/migrate/001_create_countries.rb to:

class CreateCountries < ActiveRecord::Migration def self.up create_table :countries, :options => 'engine=MyISAM' do |t| t.column :title, :string, :limit => 100 end end def self.down drop_table :countries end end

If you are working on a linux box, make sure the sockets are set in config/database.yml; do not include the sockets if you are working on a windows box. Also add utf8 as the encoding (comments have been ignored):

development: adapter: mysql database: ims_development username: root password: host: localhost socket: /var/run/mysqld/mysqld.sock encoding: utf8 test: adapter: mysql database: ims_test username: root password: host: localhost socket: /var/run/mysqld/mysqld.sock encoding: utf8 production: adapter: mysql database: ims_production username: root password: host: localhost socket: /var/run/mysqld/mysqld.sock encoding: utf8

Run the database migration by typing:

rake db:migrate

Output:

== CreateCountries: migrating ========================== -- create_table(:countries, {:options=>"engine=MyISAM"}) -> 0.1300s == CreateCountries: migrated (0.1300s) =================

Testing the Output

Test in browser by typing into the address bar:

http://localhost:3000/countries

Create a country and test xml response by typing into the address bar:

http://localhost:3000/countries.xml

and

http://localhost:3000/countries/1

and

http://localhost:3000/countries/1.xml

Changing the Generated Code

To simplify the display of all countries, one can make the country name a link by updating to app/views/countries/index.rhtml to:

<h1>Listing countries</h1> <table border="1"> <tr> <th>Title</th> </tr> <% for country in @countries %> <tr> <td><%= link_to h(country.title), country_path(country) %></td> </tr> <% end %> </table> <br /> <%= link_to 'New country', new_country_path %>

Now to add a link to destroy to app/views/countries/show.rhtml, update to:

<p> <b>Title:</b> <%=h @country.title %> </p> <%= link_to 'Edit', edit_country_path(@country) %> | <%= link_to 'Destroy', country_path(@country), :confirm => 'Are you sure?', :method => :delete %> | <%= link_to 'Back', countries_path %>

Next recommended article to read: Active Record Associations and Validations with Resouce Mapping

Technorati tags: , , ,

Liked it? !

Posted on February 18th | 2 comments | Filed Under: Ruby on Rails