Ruby on rails - Oneonta

bricklayerbelchedInternet και Εφαρμογές Web

5 Φεβ 2013 (πριν από 4 χρόνια και 9 μήνες)

138 εμφανίσεις

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

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


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

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"


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


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>


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]


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 "<br />";

}

?>



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

end


A login generator


Some notes for a prewritten login
generator for rails are at


http://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'] && @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

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>



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>


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)