37
Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Ruby on rails

This ppt contains a pot pourri of information including some sql, some

php, some instant rails and so on

Page 2: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Where to get rails

• Ruby, gems, and rails can be downloaded separately, from a variety of sources, in particular RubyForge.

• I downloaded ruby to my desktop so I could conveniently develop applications

• I downloaded the “Instant Rails” distribution which comes with Ruby, PHP, and MySQL and the Apache server from https://rubyforge.org/projects/instantrails/

• This simply gets unzipped. Use 7-zip rather than winzip. When you run it, you get an interface to start/stop the apache and mysql servers.

• Not detailed here –you may separately need to install ruby gems – the ruby package manager. It is available from rubyforge.org.. After extraction, run prompt>ruby setup.rb

Page 3: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

bookmarks

• Among those you may want to mark are tha rails api pages:

http://api.rubyonrails.org/

• The Railspace book has a site at

http://railsspace.com/book

Page 4: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Instant Rails looks like this:The I in lefthand corner is clicked to open menus

Page 5: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Alternatively

• For windows, download Ruby (windows installer) at RubyForge.org

• Install RubyGems (a zip file at RubyForge)

• After extraction, run prompt>ruby setup.rb

• Install Rails from the commandlineprompt>gem install rails –include dependencies

Page 6: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Remarks on rails

• Rails is written in and for ruby.

• Rails is a net programming framework, that builds various directories and files common to an MVC architecture.

• You modify and add to these to customize your development.

• Text walkthrough seems mostly accurate. I’ve make some screen shots/notes/etc.

Page 7: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

mvc

browser

controller

model

database

view

Page 8: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

A convention

• For a site named MySite, the project would be created with lowercase and underscore, as my_site.

• You build a subdirectory below rails_apps and run rails from there.

Page 9: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Directories: where do things go?

• app: views, models, helpers, controllers

• test: unit, integration,functional, fixtures

• public: stylesheets, javascripts, images

• lib: tasks

• db: migrate

• config: environments

Page 10: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Run mongrel: ruby script/server from rails1 directory

Page 11: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Mongrel at port 3000

Page 12: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Converting a ruby application to a rails application

Page 13: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Summary of steps• Add a directory under rail_apps (I called mine pfix) and change to

this directory:Mkdir pfixCd pfix• Then run rails in this directory to create files and folders using a new

subdirectory name, like postfix.• I used the_form.rhtml and result.rhtml files for the view.• Evalcontroller is basically the postfix program.• The stack class needs to be put in this controller directory as well.• I added a variable in the class @the_error and a method errors in

the stack class which returns @error, a field value, to the evalcontroller.

• The original line, the answer and any error appear in the result form.

Page 14: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

After clicking evaluate postfix button

Page 15: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

An expression with errors

Page 16: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Notes

• Evalcontroller is in this slide’s notes

• Stack class changes left as exercise

Page 17: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

The_form for postfix<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<!-- the_form.rhtml This describes a postfix form page> --><html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Postfix Form </title> </head> <body> <h2> Welcome to postfix evaluator </h2><!-- The next line gives the address of the CGI program --> <form action = "result" method = "post"> <table> <tr> <td> Enter postfix expression: </td> <td> <input type = "text" name = "expression" size = "70" /> </td> </tr> </table> <p /><!-- The submit and reset buttons --> <p> <input type = "submit" value = "Evaluate Postfix" /> <input type = "reset" value = "Clear Form" /> </p> </form> </body></html>

Page 18: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Result.rhtml<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><!-- result.rhtml - result view for the postfix application --><html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> result.rhtml </title> <head> <body> <table> <tr> <td> postfix expression: </td> <td> <%= @line %> </td> </tr> <tr> <td> Evaluated: </td> <td> <%= @answer %> </td> </tr> <tr>

<td> any errors: </td> <td> <%= @the_error %> </td>

</tr> </table> </body></html>

Page 19: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

On the DOS window where mongrel is running…

Processing EvalController#the_form (for 127.0.0.1 at 2007-12-26 18:25:10) [GET] Session ID: a97b5ba4899036b6dc4239629d8909c3 Parameters: {"action"=>"the_form", "controller"=>"eval"}Rendering eval/the_formCompleted in 0.00010 (10000 reqs/sec) | Rendering: 0.00000 (0%) | 200 OK [http://localhost/eval/the_form]form]

Processing EvalController#result (for 127.0.0.1 at 2007-12-26 18:25:39) [POST] Session ID: a97b5ba4899036b6dc4239629d8909c3 Parameters: {"action"=>"result", "expression"=>"10 10 20 * + 70 /", "controller"=>"eval"}Rendering eval/resultCompleted in 0.00010 (10000 reqs/sec) | Rendering: 0.00000 (0%) | 200 OK [http://localhost/eval/result]esult]

Page 20: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Access to phpMyAdmin

• Click the I

• Select configure

• Select database

Page 21: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

phpMyAdmin in instantrails: go to configure/database from I menu

Page 22: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Unrelated to ruby: accessing mysql from php in instantrails distribution of mysql

<?php// Make a MySQL Connectionmysql_connect("localhost", "root", "") or die(mysql_error());mysql_select_db("test") or die(mysql_error());// Retrieve all the data from the "example" table$result = mysql_query("SELECT * FROM Students")or die(mysql_error());// store the record of the "example" table into $row//$row = mysql_fetch_array( $result );// Print out the contents of the entry//$result = mysql_query($query) or die(mysql_error());while($row = mysql_fetch_array($result)){

echo $row['name']. " - ". $row['id'];echo "<br />";

}?>

Page 23: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Note url…put php files in www directory

Page 24: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

RadRails

• Radrails is a 43 mb download from SourceForge

• It is a rails IDE

Page 25: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Radrails looks like this – I wound up not using this environment

Page 26: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Generating controller for rail_space

• C:\InstantRails\rails_apps\RailSpace\rail_space>ruby script/generate controller site index about help

• exists app/controllers/• exists app/helpers/• create app/views/site• exists test/functional/• create app/controllers/site_controller.rb• create test/functional/site_controller_test.rb• create app/helpers/site_helper.rb• create app/views/site/index.rhtml• create app/views/site/about.rhtml• create app/views/site/help.rhtml

• C:\InstantRails\rails_apps\RailSpace\rail_space>

Page 27: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Site files

• Note the app/views/site (index, about, help) rhtml files are generated automatically and correspond to (next slide) methods of site controller

Page 28: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Site_Controller.rb

class SiteController < ApplicationController

def index end

def about end

def help endend

Page 29: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

A login generator

• Some notes for a prewritten login generator for rails are at

• http://wiki.rubyonrails.org/rails/pages/LoginGenerator

Page 30: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Notes on setting up login• Edit app/controllers/application.rb

require 'login_system' class ApplicationController < ActionController::Base include LoginSystem model :user end

• Edit app/controllers/account_controller.rb to set who if anyone can delete users. Until they have admin roles, most people can set it so only account holders can delete their account:def delete

if params['id'] && @session['user'] && @session['user'].id == params['id'] • Edit your own controllers now, and add the line

before_filter :login_required to the inside of the class so that it ends up looking something like this:class AllMySecretsController < ApplicationController

before_filter :login_required def show_one_secret ... end end

• Of course, I sometimes don’t want every method hidden behind the iron curtain, so I can exclude some of them (such as show_one_secret above) from being protected like so:class AllMySecretsController < ApplicationController

before_filter :login_required, :except => [ :show_one_secret ] def show_one_secret ... end end

Page 31: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Installing some gems – just the last is needed for this login generator

C:\InstantRails\ruby>gem install --source http://gems.rubyforge.org localization_generatorBulk updating Gem source index for: http://gems.rubyforge.orgSuccessfully installed rake-0.8.1Successfully installed localization_generator-1.0.82 gems installedInstalling ri documentation for rake-0.8.1...Installing RDoc documentation for rake-0.8.1...C:\InstantRails\ruby>gem install --source http://gems.rubyforge.org salted_login_generatorSuccessfully installed salted_login_generator-2.0.21 gem installedC:\InstantRails\ruby>gem install --source http://gems.rubyforge.org login_generatorSuccessfully installed login_generator-1.2.21 gem installedC:\InstantRails\ruby>

Page 32: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

The login generatorC:\InstantRails\rails_apps>mkdir loginapp

C:\InstantRails\rails_apps>cd loginapp

C:\InstantRails\rails_apps\loginapp>rails MyLogin create create app/controllers create app/helpers create app/models create app/views/layouts …

C:\InstantRails\rails_apps\loginapp\MyLogin>gem install login_generatorBulk updating Gem source index for: http://gems.rubyforge.orgSuccessfully installed login_generator-1.2.2

C:\InstantRails\rails_apps\loginapp\MyLogin>ruby script/generate login Acco create lib/login_system.rb create app/controllers/account_controller.rb create test/functional/account_controller_test.rb create app/helpers/account_helper.rb create app/models/user.rb create test/unit/user_test.rb create test/fixtures/users.yml create app/views/layouts/scaffold.rhtml create public/stylesheets/scaffold.css create app/views/account create app/views/account/welcome.rhtml create app/views/account/login.rhtml create app/views/account/logout.rhtml create app/views/account/signup.rhtml create README_LOGIN

C:\InstantRails\rails_apps\loginapp\MyLogin>

Page 33: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Be sure to create the correct database for this app

• Rails uses test, production and development databases for each app.

• Here use the database

mylogin_development

• Create the table users (see next slide)

• replace mylogin by your app name

Page 34: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Create a user table in mysql

• here’s a script you can run in phpmyadmin:

CREATE TABLE users ( id int(11) NOT NULL auto_increment, login varchar(80) default NULL, password varchar(40) default NULL, PRIMARY KEY (id) );

Page 35: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Running this app

Page 36: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

MrEd with pw horse

Page 37: Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on

Then you get this (there’s some work to do still to arrange redirection)