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 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
bookmarks Among those you may want to mark are tha rails api pages: The Railspace book has a site at
Instant Rails looks like this:The I in lefthand corner is clicked to open menus
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
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.
mvc browser controller model database view
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.
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
Run mongrel: ruby script/server from rails1 directory
Mongrel at port 3000
Converting a ruby application to a rails application
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 and a method errors in the stack class which a field value, to the evalcontroller. The original line, the answer and any error appear in the result form.
After clicking evaluate postfix button
An expression with errors
Notes Evalcontroller is in this slide’s notes Stack class changes left as exercise
The_form for postfix <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" " --> Postfix Form Welcome to postfix evaluator Enter postfix expression:
Result.rhtml <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" " <!-- result.rhtml - result view for the postfix application --> result.rhtml postfix expression: Evaluated: any errors:
On the DOS window where mongrel is running… Processing EvalController#the_form (for at :25:10) [GET] Session ID: a97b5ba b6dc d8909c3 Parameters: {"action"=>"the_form", "controller"=>"eval"} Rendering eval/the_form Completed in (10000 reqs/sec) | Rendering: (0%) | 200 OK [ /localhost/eval/the_form] form] Processing EvalController#result (for at :25:39) [POST] Session ID: a97b5ba b6dc d8909c3 Parameters: {"action"=>"result", "expression"=>" * + 70 /", "controlle r"=>"eval"} Rendering eval/result Completed in (10000 reqs/sec) | Rendering: (0%) | 200 OK [ /localhost/eval/result] esult]
Access to phpMyAdmin Click the I Select configure Select database
phpMyAdmin in instantrails: go to configure/database from I menu
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 " "; } ?>
Note url…put php files in www directory
RadRails Radrails is a 43 mb download from SourceForge It is a rails IDE
Radrails looks like this – I wound up not using this environment
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>
Site files Note the app/views/site (index, about, help) rhtml files are generated automatically and correspond to (next slide) methods of site controller
Site_Controller.rb class SiteController < ApplicationController def index end def about end def help end
A login generator Some notes for a prewritten login generator for rails are at Generatorhttp://wiki.rubyonrails.org/rails/pages/Login Generator
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'] == 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
Installing some gems – just the last is needed for this login generator C:\InstantRails\ruby>gem install --source localization _generator Bulk updating Gem source index for: Successfully installed rake Successfully installed localization_generator gems installed Installing ri documentation for rake Installing RDoc documentation for rake C:\InstantRails\ruby>gem install --source salted_login _generator Successfully installed salted_login_generator gem installed C:\InstantRails\ruby>gem install --source login_genera tor Successfully installed login_generator gem installed C:\InstantRails\ruby>
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: Successfully installed login_generator 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>
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
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) );
Running this app
MrEd with pw horse
Then you get this (there’s some work to do still to arrange redirection)