Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Ruby on rails This ppt contains a pot pourri of information including some sql, some php, some instant rails and so on."— Presentation transcript:

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

2 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/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

3 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

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

5 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 commandline prompt>gem install rails –include dependencies

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

7 mvc browser controller model database view

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

9 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

10 Run mongrel: ruby script/server from rails1 directory

11 Mongrel at port 3000

12 Converting a ruby application to a rails application

13 Summary of steps Add a directory under rail_apps (I called mine pfix) and change to this directory: Mkdir pfix Cd 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.

14 After clicking evaluate postfix button

15 An expression with errors

16 Notes Evalcontroller is in this slide’s notes Stack class changes left as exercise

17 The_form for postfix <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> --> Postfix Form Welcome to postfix evaluator Enter postfix expression:

18 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 --> result.rhtml postfix expression: Evaluated: any errors:

19 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_form Completed 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 /", "controlle r"=>"eval"} Rendering eval/result Completed in 0.00010 (10000 reqs/sec) | Rendering: 0.00000 (0%) | 200 OK [http:/ /localhost/eval/result] esult]

20 Access to phpMyAdmin Click the I Select configure Select database

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

22 Unrelated to ruby: accessing mysql from php in instantrails distribution of mysql <?php // Make a MySQL Connection mysql_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 " "; } ?>

23 Note url…put php files in www directory

24 RadRails Radrails is a 43 mb download from SourceForge It is a rails IDE

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

26 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>

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

28 Site_Controller.rb class SiteController < ApplicationController def index end def about end def help end

29 A login generator Some notes for a prewritten login generator for rails are at http://wiki.rubyonrails.org/rails/pages/Login Generatorhttp://wiki.rubyonrails.org/rails/pages/Login Generator

30 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 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

31 Installing some gems – just the last is needed for this login generator C:\InstantRails\ruby>gem install --source http://gems.rubyforge.org localization _generator Bulk updating Gem source index for: http://gems.rubyforge.org Successfully installed rake-0.8.1 Successfully installed localization_generator-1.0.8 2 gems installed Installing 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 _generator Successfully installed salted_login_generator-2.0.2 1 gem installed C:\InstantRails\ruby>gem install --source http://gems.rubyforge.org login_genera tor Successfully installed login_generator-1.2.2 1 gem installed C:\InstantRails\ruby>

32 The login generator C:\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_generator Bulk updating Gem source index for: http://gems.rubyforge.org Successfully 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>

33 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

34 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) );

35 Running this app

36 MrEd with pw horse

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


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

Similar presentations


Ads by Google