Home arrow Ruby-on-Rails arrow Page 3 - Adding Authors to an Online Bookstore
RUBY-ON-RAILS

Adding Authors to an Online Bookstore


In this second part of a four-part article series on creating an online bookstore application with Ruby on Rails, you'll learn how to run a migration script and unit tests, create the controller, and more. This article is excerpted from chapter two of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
May 12, 2010
TABLE OF CONTENTS:
  1. · Adding Authors to an Online Bookstore
  2. · Running Unit Tests
  3. · Creating the Controller
  4. · Implementing the User Stories

print this article
SEARCH DEVARTICLES

Adding Authors to an Online Bookstore - Creating the Controller
(Page 3 of 4 )

As explained in Chapter 1, a controller is the central part of an application that takes care of receiving the user request, modifying data through the model part, and finally either rendering a view template or redirecting the user to another URL. Now that we have the ActiveRecord model for authors in place, we need a controller that implements the administration interface for authors. From that last sentence, we can already pick a good name for the controller: admin/author.  

You create a controller by using the samescript/generatecommand that you use to create a model. You can also give thegenerate controllercommand names of the actions you want the controller to implement—in this case,new,create,edit,update,destroy,show, andindex.

$ script/generate controller 'admin/author' new create edit update➥
                 destroy show index

--------------------------------------------
  create  app/controllers/admin
  create  app/helpers/admin
  create  app/views/admin/author
  create  test/functional/admin
  create app/controllers/admin/author_controller.rb 
  create test/functional/admin/author_controller_test.rb
  create  app/helpers/admin/author_helper.rb
  create  app/views/admin/author/new.rhtml 
  create  app/views/admin/author/create.rhtml
  create  app/views/admin/author/edit.rhtml
  create  app/views/admin/author/update.rhtml
  create app/views/admin/author/destroy.rhtml
  create  app/views/admin/author/show.rhtml
  create  app/views/admin/author/index.rhtml
--------------------------------------------

By usingadmin/authorinstead of justauthoras the controller name, we put theauthorcontroller inside anadminsubdirectory. The newAuthorControllerclass is also set inside a module calledAdmin. This way, we can later implement other administrative controllers under the same module and make them share common code such as access control.

Just as for models, thegeneratecommand created a controller stub and empty views for all actions for us. It also created a functional test file for our controller.


Note  If you haven’t already done so, now would be a good time to put your code under source control. We prefer Subversion, as it’s the norm in the Rails world, but the most important thing is that you use some kind of source control management. You wouldn’t want to tell George that you lost a whole day’s worth of code, would you?



blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials