Rails and Grails

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

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

320 εμφανίσεις

Rails and Grails

To get started


Make sure you have java installed


You can get the
sdk

and
jre

at:
http://
www.oracle.com/technetwork/java/javase/downloads/
java
-
se
-
jdk
-
7
-
download
-
432154.html


Install the
jdk

and not just the
jre

Ruby on Rails


Context


Website development is highly repetitive


The client side has been addressed with a number of tools that
support drag and drop, adjustable technology, often xml
-
based


But the rest of the process remained fairly
tedius


Goal of various web development frameworks


Provide a top down approach to building
db
-
centric web apps


Remove a lot of configuration detail work


Generate the core of the back end code using MVC


Hide the
db

from the developer and provide a more uniform
approach, language
-
wise, for building a web app


Tradeoff


programmer loses a lot of control over the
architecture of the web app


Install Rails



Go
to
http://www.ruby
-
lang.org/en/downloads
/


Choose your platform and


install the ruby language


install “ruby gems”


windows, use
the
installer at
http
://rubyinstaller.org
/


Install
RubyMine
:
http://www.jetbrains.com/ruby/quickstart
/


Go to Settings on
RubyMine



Tell it where your ruby installation is


Install the gem called “bundler”


It helps install other gems


Run update on all your gems



Create a project


Use File to create a new “rails application” in
RubyMine


Set it up for
mysql



RubyMine

will do this for you


Call it
first_project

-

You should get this:

Your project folder


App folder


Has your controllers, models, and views in it


Remember to save it frequently


Config

folder


Has
database.yml

file


note that the default is
sqlite


This is why you had
RubyMine

use
mysql


Notice that there are 3 databases in an official
RonR

app


Development, test, production


Use development, you don’t need to build the other
dbs

Your database


Turn on your
mysql

and apache


Create a database called
first_project_development


No need to create any tables…


You may need to put this in your ruby/bin folder:


You can get it here






http
://dev.mysql.com/get/Downloads/Connector
-
C/mysql
-
connector
-
c
-
noinstall
-
6.0.2
-
win32.zip/from/pick


Run your app

You should get this:


Click on the blue words

and get:

MVC
RonR

subdirectories


The

controllers

subdirectory.
A controller handles a web
request from the user.


The

views

subdirectory holds the display templates to fill in
with
data and return data
to the user's browser.


The

models

subdirectory holds the classes
that define the data
being manipulated


The

helpers

subdirectory
holds
helper classes used to assist
the model, view, and controller classes.
The idea is to keep the
m, v, and c classes minimal.


Rails tutorial: Assignment 9

Due on Dec 11.


See:
http://
ruby.railstutorial.org/ruby
-
on
-
rails
-
tutorial
-
book?version=3.2

for a very detailed tutorial


And see:
https://
github.com/perfectionist/sample_project/wiki/Using
-
RubyMine
-
IDE
-
for
-
Ruby
-
on
-
Rails
-
Tutorial

for instructions on how to
use
RubyMine

for the tutorial


Tutorial assignment: build the demo app described in chapter 2 of
the
tutorial document:
http://
ruby.railstutorial.org/chapters/a
-
demo
-
app?version=3.2#top


Hand in a zipped up project folder. I don’t need the database.
Include a screen snapshot of your project folder in
RubyMine
.


Important: this material will be on the final.


Note: if you have problems with any details of the tutorial you can
skip them


the important thing is to learn the concepts and to do
most of the tutorial.


Some things to think about as
you do the
RonR

tutorial…


What is Active Record?


An abstraction layer that sits on top of relational databases


Provides an easy way to move between relational platforms


Relies on a single .
yml

file


The idea is that you do not write SQL, at least not directly


You can
retrieve objects:
http://
guides.rubyonrails.org/active_record_querying.html


You can add, change, delete tables using

migrations”




http
://
guides.rubyonrails.org/migrations.html


NOTE: The link to the guides as a
whole is:
http://guides.rubyonrails.org
/



What do you think of the Active Record paradigm?

Active records


This is a form of object to relational mapping


Active record connects ruby classes to
db

tables


It is installed with “gem install
activerecord
”, or via
RubyMine


See:
http://
api.rubyonrails.org/files/activerecord/README_rdoc.html


The classes are the “models” that map to tables


“Views” are html pages with embedded ruby


“Controllers” are ruby scripts


Restrictions (conventions)


Class names are singular


Table names are plural


Tables contain and identity column named id


If you use underscores _ in your table name, you leave them out on
the class name


Active record supports CRUD


Create, read, update, delete primitives


More Ruby on Rails questions


What are “migrations”?


They are a structured and database product


Database independent (e.g., MySQL versus Oracle) way of
creating and maintaining a database while you build a site


Look at:
http://
guides.rubyonrails.org/migrations.html


What is Rake?


It is a build tool inspired by Make


What
is
“scaffolding”?


This is what gives you the framework of an entire website in a
snap, making it easy to orient yourself and show something to a
customer early on in the website development process


You can create basic templates for views, controllers, and models

… What do you think of migrations and scaffolding?


Active records primitives


create_table


change_table


drop_table


add_column


change_column


rename_column


remove_column


add_index


remove_index


Important construct:
generators


These are rails commands that can be used to create pieces of
a website


Generators often initiate other operations


Example

$ rails generate scaffold User
name:string



results in the following generated code:


invoke


active_record





db
/migrate/20091120125558_create_users.rb


create


app/models/
user.rb



invoke


test_unit




create


test/unit/
user_test.rb




create


test/fixtures/
users.yml



route


resources :users


Example continued

invoke


scaffold_controller


create


app/controllers/
users_controller.rb


invoke


erb


create


app/views/users


create


app/views/users/
index.html.erb


create


app/views/users/
edit.html.erb


create


app/views/users/
show.html.erb


create


app/views/users/
new.html.erb



create


app/views/users/_
form.html.erb


invoke


test_unit


create


test/functional/
users_controller_test.rb


invoke


helper


create


app/helpers/
users_helper.rb


invoke


test_unit


create


test/unit/helpers/
users_helper_test.rb


invoke


stylesheets


create


app/assets/
stylesheets
/scaffold.css


Ruby on rails app servers


Native ruby webserver:
Webrick

and needs no configuration,
and it is really only a web server (HTTP)


Lighttpd

is a web server that focuses on speed and low
memory usage


You can use Apache


There is growing use of
Phusion
:
https://www.phusionpassenger.com
/


Installing groovy and grails


Go to
http://groovy.codehaus.org
/

and install groovy


Make sure to let your OS create a system variable so groovy can be
found


Go to
http://
grails.org/download

and download grails, then open it
up in one of your user folders (e.g., documents)


Open up
Intellij


Turn on the grails plugin in the plugins settings in File/Settings


Create a grails project; you will be asked to point
Intellij

to your grails
folder


You will be asked if you are developing an app or a plugin; choose
app


By default, Grails uses an in memory database called HSQLDB.



Hibernate is a widely used object/relational mapping layer that is
similar in purpose to Active Record


G
rails tutorial: Assignment 10

Due on Dec. 11


You will discover a system of folders has been created, and it
looks very much like ruby on rails


The
datasource.groovy

file is where you set up
mysql

or other
database


The tutorial to
follow is at:
http://
wiki.jetbrains.net/intellij/Creating_a_simple_Grails_app
lication_with_IntelliJ_IDEA


This is much simpler than the
RonR

tutorial.


Submit: your final grails project, zipped up. I do not need the
database. Include a screen snapshot of your Grails project in
Intellij
.


Important: this material will be on the final.


…something to think about:



how is Grails different from Ruby on Rails?

Extra credit on Grails
projecct


Get your grails project working with
mysql

and show it to me!


Hint:


You need to change the
DataSource.groovy

code


And
---

you need to change
BuildConfig.groovy

code