1 / 27

Ruby on Rails+MySQL

Learn to create a Rails app, generate controllers, and set up a MySQL database for a car directory project. Includes step-by-step instructions and code snippets.

Download Presentation

Ruby on Rails+MySQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby on Rails+MySQL

  2. Sebesta corvette example • In rails_apps create a directory (car_app), change to this dir and run rails (rails cars) Rails_apps cars Car_app

  3. 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>

  4. Building tables • I used phpmyadmin to build the tables. • The text uses scripts in mysql to generate the tables.

  5. main/welcome

  6. Requesting all coupes from 1970 to 2005

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

  8. Where to put the files • Put controllers in controllers • Put rhtml (or directories with display.rhtml) in views • Models dir

  9. controllers

  10. views

  11. Create a new database, cars_development

  12. Phpmyadmin from rails

  13. Looking at a table

  14. An app dir in a rails application

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

  16. User “secretariat” created

  17. Secretariat in db

  18. So what all is in this app?

  19. 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

  20. site_controller class SiteController < ApplicationController def index @title="Welcome to 'My' RailSpace!" end def about @title="About 'My' RailSpace!" end def help @title="Help for 'My' RailSpace!" end end

  21. user_controller class 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 end end

  22. app/models- users.rb in notes

  23. App/views/user

  24. 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 %>

  25. 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>

  26. Now…logging in

  27. And then…

More Related