RubyOnRails

From $1

On Win32 - with apache2 in c:\usr\local\apache

Base Install and Test
  • gem install rails (installs 0.8.5)
  • rails c:/usr/local/apache/htdocs/railstest
    • I'll refer to this directory as $RT
    • Note: it begins by doing a rm -rf of $RT!
  • Open a browser to "http://localhost/railstest/public/index.html":http://localhost/railstest/public/index.html. (You should see a big "Congratulations" message.)

  • Ensure that the following lines are active in httpd.conf:
    • AddHandler cgi-script .cgi

    • Ensure that ExecCGI is on the options statement for htdocs
    • Ensure that mod_rewrite.so is un-commented.

h5. The Cookbook Demo Program

  • _Note to self: The word is recipe and not recipie._
  • rails c:/rails/cookbook

  • cd \rails\cookbook
  • ruby script\server
  • Open a browser to "http://localhost:3000":http://localhost:3000

  • Open a mysql prompt:
    • create database cookbook;
    • use cookbook;
    • create table recipes (id int NOT null primary key, title varchar(255) not null, instructions text, description varchar(255), date date, category_id int(6));
    • create table categories (id int NOT null primary key, name varchar(50));
  • ruby script\generate model recipe
    • Note: Singular model recipe maps to plural table recipes
  • ruby script\generate controller Recipe
  • Edit recipe_controller.rb

scaffold:recipe

def list
  @recipes = Recipe.find_all
end

def edit
  @recipe = Recipe.find(@params(["id"])
  @categories = Category.find_all
end

<html>
<head>
<title>All Recipes</title>
</head>
<body>

<h1>Online Cookbook - All Recipes</h1>
<table border="1">
 <tr>
  <td width="80%"><p align="center"><i><b>Recipe</b></i></td>
  <td width="20%"><p align="center"><i><b>Date</b></i></td>
 </tr>

 <% @recipes.each do |recipe| %>
  <tr>
   <td><%= link_to recipe.title, :action => "show", :id => recipe.id %></td>
   <td><%= recipe.date %></td>
  </tr>
 <% end %>
</table>
<p><%= link_to "Create new recipe", :action => "new" %></p>

</body>
</html>
  • ruby script\generate controller Category
  • ruby script\generate model Category
  • edit category_controller.rb

scaffold:category

belongs_to:category
  • Edit models/category.rb

has_many:recipies
Tags:
none
 
Images (0)
 
Comments (0)
You must login to post a comment.