Rails Workshop Lesson Plan

This accompanies the rails notes page.

  1. What is Rails discussion.

  2. Download/install InstantRails

    1. Unzip InstantRails and move the InstantRails folder to the C:/ folder.

    2. Instant Rails will detect that it is being started from a new directory and ask if you want to have it update the paths in the all of the configuration files… Just say yes.

    3. Click on the I button to drop down the main menu and select Configure > Windows Hosts file.

    4. In the editor that pops up, add this line to the end of the file:

      127.0.0.1   blogr.com

    5. Save the change and exit the editor.

    6. In the main menu, select Rails Applications > Manage Rails Applications…

  3. Run rails blogr from command line and enter the newly-created blogr directory with cd blogr.

  4. Start local mysql server and create the three local databases,

    In MySQL language, this is

    CREATE DATABASE blogr_development;
    CREATE DATABASE blogr_test;
    CREATE DATABASE blogr_production;

  5. Modify blogr/config/database.yml to reflect the new local server. E.g.,

    development:
      adapter: mysql
      database: blogr_development
      socket: /Applications/MAMP/tmp/mysql/mysql.sock
      user: root
      password: root

  6. Run ruby script/server to start the local WEBRick server

  7. Navigate to http://localhost:3000 and make sure everything checks out: click the “About your application’s environment” link and ensure you don’t get any warnings.

  8. Stop the WEBRick server by going to the console where you started it and hitting Control+C.

  9. MVC Discussion

    1. Schema discussion:

      stories: authors: id +-> id title | title slug | email story | password created_at | created_at updated_at | author_id <——-+

    2. Generate the scaffolding for stories by running

      ruby script/generate scaffold Story \ title:string \ slug:string \ story:text \ author_id:integer

    Note that id, created_at, and updated_at are free.

    1. Migrations discussion

    2. Run the migration to add in the new stories columns by running rake db:migrate.

    3. Scaffolding discussion

    4. Navigate to http://localhost:3000/stories to ensure you get a “Listing stories” page

    5. Add a missing column, rating by using Migrations. Run ruby script/generate migration add_story_rating. Modify this Migration, blogr/db/migrate/002_add_story_rating.rb, to be

      {{lang:ruby}} class AddStoryRating < ActiveRecord::Migration def self.up add_column :stories, :rating, :integer, :default => 3 end

      def self.down remvoe_column :stories, :rating end end

    6. Migrate the database by running rake db:migrate.

    7. Model association discussions:

    Also discuss the columns that are added to the tables as a result of these relationships.

    1. Update the models

    blogr/app/models/story.rb becomes

    class Story < ActiveRecord::Base
      belongs_to :author
    end

    blogr/app/models/author.rb becomes

    class Author < ActiveRecord::Base
      has_many :stories
    end

    1. Generate scaffolding for authors

Author:  Ryan Timmons
Last Modified:  06 August 2008 15:23:13 PDT
URL:  http://uwwebpub.com/

See Original Markdown