ruby on rails+mysql. sebesta corvette example in rails_apps create a directory (car_app), change to...

27
Ruby on Rails+MySQL

Upload: marcia-mccoy

Post on 19-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Ruby on Rails+MySQL

Page 2: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Sebesta corvette example

• In rails_apps create a directory (car_app), change to this dir and run rails (rails cars)

Rails_apps

Car_app cars

Page 3: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Go to cars dir and type: ruby script/generate controller main

C:\InstantRails\rails_apps>mkdir car_app

C:\InstantRails\rails_apps>cd car_app

C:\InstantRails\rails_apps\car_app>rails cars create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments create components create db create doc create lib create lib/tasks … C:\InstantRails\rails_apps\car_app>cd cars

C:\InstantRails\rails_apps\car_app\cars>ruby script/generate controller main exists app/controllers/ exists app/helpers/ create app/views/main exists test/functional/ create app/controllers/main_controller.rb create test/functional/main_controller_test.rb create app/helpers/main_helper.rb

C:\InstantRails\rails_apps\car_app\cars>

Page 4: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Building tables

• I used phpmyadmin to build the tables.

• The text uses scripts in mysql to generate the tables.

Page 5: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

main/welcome

Page 6: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Requesting all coupes from 1970 to 2005

Page 7: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Source files for cars app (with little guidance on where to put them)

Page 8: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Where to put the files

• Put controllers in controllers

• Put rhtml (or directories with display.rhtml) in views

• Models dir

Page 9: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

controllers

Page 10: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

views

Page 11: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Create a new database, cars_development

Page 12: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Phpmyadmin from rails

Page 13: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Looking at a table

Page 14: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

An app dir in a rails application

Page 15: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Trying to register to be a member returns “user created” if no errors

Page 16: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

User “secretariat” created

Page 17: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Secretariat in db

Page 18: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

So what all is in this app?

Page 19: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

application.rb

# Filters added to this controller apply to all controllers in the application.

# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base

# Pick a unique cookie name to distinguish our session data from others'

session :session_key => '_rail_space_session_id'end

Page 20: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

site_controllerclass SiteController < ApplicationController

def index @title="Welcome to 'My' RailSpace!" end

def about @title="About 'My' RailSpace!" end

def help @title="Help for 'My' RailSpace!" endend

Page 21: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

user_controllerclass UserController < ApplicationController

def index end

def register @title = "Register" if request.post? @user = User.new(params[:user]) if @user.save render :text => "User created!" end end endend

Page 22: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

app/models- users.rb in notes

Page 23: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

App/views/user

Page 24: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

register.rhtml<h2>Register</h2> <% form_for :user do |form| %> <fieldset> <legend>Enter Your Details</legend> <%= error_messages_for "user" %> <div class="form_row"> <label for="screen_name">Screen name:</label> <%= form.text_field :screen_name, :size => User::SCREEN_NAME_SIZE, :maxlength => User::SCREEN_NAME_MAX_LENGTH %> </div> <div class="form_row"> <label for="email">Email:</label> <%= form.text_field :email, :size => User::EMAIL_SIZE, :maxlength => User::EMAIL_MAX_LENGTH %> </div> <div class="form_row"> <label for="password">Password:</label> <%= form.password_field :password, :size => User::PASSWORD_SIZE, :maxlength => User::PASSWORD_MAX_LENGTH %> </div> <div class="form_row"> <%= submit_tag "Register!", :class => "submit" %> </div> </fieldset> <% end %>

Page 25: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

login.rhtml<h2>Log in</h2><% form_for :user do |form| %><fieldset> <legend>Enter Your Details</legend> <div class="form_row"> <label for="screen_name">Screen name:</label> <%= form.text_field :screen_name, :size => User::SCREEN_NAME_SIZE, :maxlength => User::SCREEN_NAME_MAX_LENGTH %> </div> <div class="form_row"> <label for="password">Password:</label> <%= form.password_field :password, :size => User::PASSWORD_SIZE, :maxlength => User::PASSWORD_MAX_LENGTH %> </div> <div class="form_row"> <%= submit_tag "Login!", :class => "submit" %> </div></fieldset><% end %><p> Not a member? <%= link_to "Register now!", :action => "register" %></p>

Page 26: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

Now…logging in

Page 27: Ruby on Rails+MySQL. Sebesta corvette example In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps Car_app

And then…