Rolling with Ruby on Rails - Part 2.pdf
(
327 KB
)
Pobierz
Rolling with Ruby on Rails - Part 2
Published on
ONLamp.com
(
http://www.onlamp.com/
)
http://www.onlamp.com/pub/a/onlamp/2005/03/03/rails.html
See this
if you're having trouble printing code examples
Rolling with Ruby on Rails, Part 2
by
Curt Hibbs
03/03/2005
Welcome back!
In
Rolling with Ruby on Rails
, I barely scratched the surface of what you can do with Ruby on Rails. I didn't talk about data
validation or database transactions, and I did not mention callbacks, unit testing, or caching. There was hardly a mention of
the many helpers that Rails includes to make your life easier. I can't really do justice to all of these topics in the space of this
article, but I will go into details on some of them and present a brief overview of the rest, with links to more detailed
information.
Also, I purposely did not go into any detail about the Ruby programming language. If you are interested in a brief treatment
of the whys behind the Ruby and Rails code that you saw in Part 1, then I highly recommend reading
Amy Hoy's
blog entry
Really Getting Started in Rails
.
Before I cover this stuff, I want to complete the homework items from the end of Part 1:
There is no longer any way to delete a recipe. Add a delete button (or link) to the edit template.
On the main recipes page, there aren't any links for the pages that let you manipulate categories. Fix that.
It would be nice to have a way to display only those recipes in a particular category. For example, maybe I'd like to see
a list of all snack recipes, or all beverage recipes. On the page that lists all recipes, make each category name a link to
a page that will display all of the recipes in that category.
One alert reader pointed out that after adding categories, it was no longer possible to add any new recipes because the
new
recipe
action (as provided by the scaffolding) had no way to assign a category, and this caused the
list recipes
action to
produce an error. This also needs fixing.
Are you ready? Let's begin!
Updating Ruby on Rails
When I wrote
Part 1
, the current version of Rails was 0.9.3. At the time of this writing, Rails is up to version 0.10.0 and has
some useful new features. I will use Rails 0.10.0 for this article. If you installed Rails after February 24, 2005, you already
have 0.10.0 installed.
Figure 1 shows how to see what RubyGems you have installed (and their version numbers). As with Part 1, I am working on
a Windows system, so you will need to translate if you use a different platform.
Figure 1. Listing installed RubyGems
Open a command window and run the command:
gem list --local
Tip: the command
gem list --remote
will show all the available RubyGems on the remote gem server on
rubyforge.org
.
If you don't have Rails 0.10.0 (or later) installed, then you will need to rerun the command:
gem install rails
MySQL security update
In Part 1, I recommended that you leave the MySQL root password blank because (at the time of writing) Rails did not
support MySQL's new password protocol. Many of you were not happy with this state of affairs, and to make matters worse,
there is now a virus that exploits password vulnerabilities in MySQL on Windows.
Happily, starting with version 0.9.4, Rails now supports the new password protocol.
New scaffold feature
Rails has a new scaffold feature, which I won't explore here, but it's cool enough that I want to make sure you know about it.
This is best illustrated by an example.
Part 1 showed how to create a recipe model and controller with the commands:
ruby script\generate model Recipe
ruby script\generate controller Recipe
I then instantiated the scaffolding by inserting
scaffold :recipe
into the
RecipeController
class. The resulting
CRUD controllers and view templates were created on the fly and are not visible for inspection.
The technique described above still works, but you now have another option. Run the command:
ruby script\generate scaffold Recipe
This generates both the model and the controller, plus it creates scaffold code and view templates for all CRUD operations.
This allows you to see the scaffold code and modify it to meet your needs. Be careful using this if you've already created
models, controllers, or view templates, as it will overwrite any existing files as it creates the scaffold code.
Completing the Recipe Application
It's time to round out the recipe application a bit. After that I'll present some other features of Rails that I'm sure you'll want
to know about.
Remember that I created my cookbook application in the directory
c:\rails\cookbook
; all paths used in this article assume
this base directory. If you chose a different location, please be sure to make the proper adjustments to the application paths
you see in this article.
If you completed Part 1 using anything earlier than Rails 0.10.0, you will need to update your cookbook application to work
with Rails 0.10.0. You can either
follow the upgrading instructions
or download
my cookbook source code
, which has the
upgrades already.
For those of you who are cheating (you know who you are) and plan to just download my source code without going through
Part 1, you will also need to create a database named
cookbook
in MySQL and populate it using
cookbook.sql
.
Creating a new recipe with a category
Because the code still relies on the scaffolding to create new recipes, there is no way to assign a category to a recipe. This
wouldn't be so bad--except that the page created to list all recipes assumes that every recipe will have a category, and it
generates an error if this is not true. That means that in the way I left things in Part 1, if you add a new recipe, you'll receive
errors while trying to list them.
The fix is to take over the new action from the scaffolding just as I showed already with the edit action. Edit
c:\rails\cookbook\app\controllers\recipe_controller.rb
and add a
new
method like in Figure 2.
Figure 2. The
Recipe
controller's
new
method
The code
@recipe = Recipe.new
creates a new, empty recipe object and assigns it to the instance variable
@recipe
.
Remember, an instance of the Recipe class represents a row in the
recipes
database table. When creating a new recipe
object, the Recipe class can assign default values for each field that the view template can use.
The Recipe model class doesn't currently set any such default values, but the view template I'll show off momentarily will
use whatever is in the
@recipe
object to initialize the display form. Later, you could add default values in the Recipe class
that will show up when you create a new recipe.
As with the edit action, this also retrieves a collection of all categories so that it can display a drop-down list of categories
from which the user can choose. The
@categories
instance variable holds this list of categories.
In the directory
c:\rails\cookbook\app\views\recipe
, create a file named
new.rhtml
that contains the HTML template shown
below. It's mostly standard HTML, with some extra code to create the
<select>
and
<option>
tags for the drop-down
list of categories:
<html>
<head>
<title>New Recipe</title>
</head>
<body>
<h1>New Recipe</h1>
<form action="/recipe/create" method="post">
<p>
<b>Title</b><br/>
<input id="recipe_title" name="recipe[title]" size="30" type="text" value=""/>
</p>
<p>
<b>Description</b><br/>
<input id="recipe_description" name="recipe[description]"
size="30" type="text" value=""/>
</p>
<p>
<b>Category:</b><br/>
<select name="recipe[category_id]">
<% @categories.each do |category| %>
<option value="<%= category.id %>">
<%= category.name %>
</option>
<% end %>
</select>
</p>
<p>
<b>Instructions</b><br/>
<textarea cols="40" id="recipe_instructions" name="recipe[instructions]"
rows="20" wrap="virtual">
</textarea>
</p>
<input type="submit" value="Create"/>
</form>
<a href="/recipe/list">Back</a>
</body>
</html>
This is not much different from the edit template from Part 1. I left out the recipe's date because I'll set it to the current date
when a user posts the form back to the web app. This ensures that the recipe's date will always be its creation date.
If you look at the form tag, you will see that this form will post to a
create
action in the
recipe
controller. Edit
c:\rails\cookbook\app\controllers\recipe_controller.rb
and add this
create
method:
def create
@recipe = Recipe.new(@params['recipe'])
@recipe.date = Date.today
if @recipe.save
redirect_to :action => 'list'
else
render_action 'new'
end
end
This method first creates a new recipe object and initializes it from the parameters posted by the form in
new.rhtml
. Then it
sets the recipe's date to today's date, and tells the recipe object to save itself to the database. If the save is successful, it
redirects to the list action that displays all recipes. If the save fails, it redirects back to the new action so the user can try
again.
Give it a try. Start the web server by opening a command window, navigating to
c:\rails\cookbook
, and running the
command
ruby script\server
. Then browse to
http://127.0.0.1:3000/recipe/new
and add a new recipe
like the one shown in Figure 3.
Figure 3. Adding a new recipe with a category
After you create the new recipe, you should see something like Figure 4.
Figure 4. List of all recipes
Deleting a recipe
If you remember from Part 1, once I took over the list action from the scaffolding I no longer had a way to delete a recipe.
Plik z chomika:
Ryjufka
Inne pliki z tego folderu:
Apress - Practical Rails Projects.pdf
(22132 KB)
Advanced Rails Recipes (2008).pdf
(3476 KB)
Agile Web Development With Ruby On Rails.pdf
(7809 KB)
Advanced Rails (2008).chm
(682 KB)
Agile Web Development with Rails 4th Edition.pdf
(6540 KB)
Inne foldery tego chomika:
[Eng] Android books
BPM Business Process Modeling
IT Ebooks Mega Pack
IT Ebooks Programming
IT epub
Zgłoś jeśli
naruszono regulamin