Ruby On Rails - University Of Utah

2y ago
32 Views
2 Downloads
1.33 MB
66 Pages
Last View : 8d ago
Last Download : 3m ago
Upload by : Camille Dion
Transcription

Ruby On RailsJames Reynolds

TodayRuby on Rails introductionRun EnviornmentsMVCA little RubyExercises

InstallationMac OS X 10.5 will include RailsMac OS X 10.4 includes RubyMost people reinstall it anywayFrom scratchDrag and drop Locomotive

DatabasesMysqlOracleSQLiteFirebirdPostgreSQLSQL Ser verDB2more

Webser versApache w/ FastCGI or MongrelLightTPDWEBrick

"IDE's"TextMate and Terminal (preferred)RadRails (Eclipse)jEditKomodoArachno RubyNetBeans IDE

Run EnvironmentsProductionDevelopmentTesting

Run EnvironmentsProductionCachedFreeze RailsShip Rails with your appetc

Run EnvironmentsDevelopmentReloads source files every timeScaffold

Run EnvironmentsTestingConnect debugger to running webappStop at breakpointsUnit testingIntegration testingFunctional testingDB is reloaded w/ each testMock and stub code

MVCFirst Described in 1979Totally ignored in web devExceptWebObjectsStrutsJavaSer ver Faces

llerresultsqueriesModel

How it worksUserReceives URLactionsProduces HTMLDeals w/data

How it worksHTML file: form button /form Userclicks submit button(sends rformSave()Model.rbsave()

Why use t file

ControllersFriendly urlsNO: http://example.com/?node 34Yes: http://example.com/blog/view/34Customizing URL’s easyconfig/routes.rb

ControllersDefault URL mappinghttp://example.com/blog/view/34controller blogaction viewid 34

ViewsCan create html, xml, and jsEasy handling of params, flash, cookies, etcAjax built in and dumb simple

Views - RHTMLMixing up % . % and % . % is a HUGEsource of bugsBe sure to put spaces around the tagsNot required, but is easier to read IMO

A little RubySymbolsDefault parameter valuesNamed parameters

Symbolsmoods { 'angry' 'red','sick' 'green','sad' 'blue' }puts "I'm feeling " moods['sick']moods { :royal 'purple',:angelic 'white',:guilty 'black' }puts "I'm feeling " moods[:royal]

Named parametersdef printParams lotsaParams {}if lotsaParams[:one]puts lotsaParams[:one]endif lotsaParams[:two]puts lotsaParams[:two]endendprintParams :one '1', :two '2'printParams :two '2', :one '1'

Common Errors!Do not press return before “ ”Wrong:printParams :one '1', :two '2'Missing or misplaced “:” causes errors!Wrong: printParams one '1',: two '2'Right: printParams :one '1', :two '2'Space or return bet ween hash/array and [.]Wrong: moods [:royal] Right: moods[:royal]

Try it ourselvesMake a webapp that says "Hello World" onthe index pageCreate the app (in Locomotive)

Hello WorldCreate the controller (in Terminal)

Hello WorldOpen the file "example controller.rb" (in TextMate)Create the index method

Hello WorldView in web browser!

Hello World via templateComment out "render text" line

Hello World via templateCreate new view file "index.rhtml"Right click on "views/example" dir

Hello World via templateInsert HTML into "index.rhtml"Preview in browser

Request ParamsPrint out the request paramsAdd this to index.rhtmlPay attention to % vs % % params.each pair do key,value % % key.to s % > % value.to s % br/ % end %

Try it ourselvesPrint out the request paramsIt looks like this(action & controller always sent as params)

CookiesChange index controller.rb to this:class ExampleController ApplicationControllerdef indexcookies[:the time] Time.now.to sredirect to :action :index2enddef index2render(:text "The cookie time is #{cookies[:the time]}")endend

Make a SessionPut this in example controller.rbclass ExampleController ApplicationControllerdef indexsession[:counter] 0session[:counter] 1enddef reset countersession[:counter] 0redirect to :action :indexendend

Make a SessionPut this in index.rhtmlYou have been here % session[:counter];pluralize(session[:counter], "time") % . br % link to "Add one", :action :index % br % link to "Reset", :action :reset counter %

Make a SessionShould look like this

Flash MessagePut this in example controller.rbclass ExampleController ApplicationControllerdef peekflash[:notice] "A BOO!"redirect to :action :indexendend

Flash MessagePut this in index.rhtml % if flash[:notice] -% div id "notice" % flash[:notice] % /div % end -% % link to "Peek?", :action :peek %

Flash MessageShould look like this

Use LayoutLook at web browser source codeNo head or body tags

Use LayoutCreate new file "example.rhtml" in "layouts"Be sure to use HTML template

Use LayoutCut the flash code out of index.rhtmlThis is all that is left: % link to "Peek?", :action :peek %

Use LayoutAdd code inside of example.rhtml’s body Yield must have % !!!The - in -% will remove the next \n. /head body % if flash[:notice] -% div id "notice" % flash[:notice] % /div % end -% % yield % /body /html

Use LayoutShould behave the same, except the sourcecode!

Use LayoutALL pages will show flash[:notice]Verify by adding a new page and changeredirect to :action :indexTo:redirect to :action :newpage

Save Form w/ SessionPut in example controller.rbclass ExampleController ApplicationControllerdef indexsession[:comment list] [ "Original item." ]enddef add itemsession[:comment list].push params[:newitem]redirect to( :action :index )endend

Save Form w/ SessionPut in index.rhtml % form tag( :action :add item ) do % % text field tag :newitem % % submit tag "Add item" % % end % ul id "my list" % session[:comment list].reverse.each do line -% li % line -% /li % end -% /ul

Save Form w/ SessionShould look like thisIf it isn’t, make sure your % is not %

Convert to AJAXChange example controller.rbclass ExampleController ApplicationControllerdef indexsession[:comment list] [ "Original item." ]enddef add itemsession[:comment list].push params[:newitem]render text " li #{params[:newitem]} /li "endend

Convert to AJAXChange index.rhtml % javascript include tag :defaults % % form remote tag( :update "my list", :url { :action :add item }, :position "top" ) do % % text field tag :newitem % % submit tag "Add item" % % end % ul id "my list" % session[:comment list].reverse.each do line -% li % line -% /li % end -% /ul

Convert to AJAXHow do you know it is AJAX?The text field didn't go blank!

No JavaScriptWhat happens if JavaScript is off?

No JavaScriptThat is unacceptable!

No JavaScriptChange example controller.rbclass ExampleController ApplicationControllerdef indexsession[:comment list] [ "Original item." ]enddef add itemsession[:comment list].push params[:newitem]if request.xhr?render text " li #{params[:newitem]} /li "elseredirect to( :action :index )endendend

No JavaScriptJavaScript off now works!

No JavaScriptJavaScript on still uses AJAX!

Use RJS fileChange example controller.rbclass ExampleController ApplicationControllerdef indexsession[:comment list] [ "Original item." ]enddef add itemsession[:comment list].push params[:newitem]redirect to( :action :index ) unless request.xhr?endend

Use RJS fileChange index.rhtml % javascript include tag :defaults % % form remote tag( :update "my list", :url { :action :add item }, :position "top" do ) % % text field tag :newitem % % submit tag "Add item" % % end % ul id "my list" % session[:comment list].reverse.each do line -% li % line -% /li % end -% /ul

Use RJS fileCreate new file "add item.rjs"

Use RJS filePut in add item.rjsThis file is RUBY code, not JavaScriptIt is converted to JavaScript by Railspage.insert html :top, "my list", " li #{params[:newitem]} /li "

Use RJS fileShould behave exactly the sameWell, why did we do that?Because we want "Magic"!To get the "Magic", all li 's need to benumbered.

Add MagicAdd id's to each itemChange index.rhtml % javascript include tag :defaults % % form remote tag( :url { :action :add item }, :position "top" ) do % % text field tag :newitem % % submit tag "Add item" % % end % ul id "my list" % session[:comment list].reverse.each with index do line, index -% li id ' % session[:comment list].length-index -% ' % line% /li % end -% /ul

Add MagicAdd id's to each itemChange add item.rjsAs one line (no returns)page.insert html :top, "my list", " liid '#{session[:comment list].length}' #{params[:newitem]} /li "

Add MagicAdd a line to add item.rjsALL AS ONE LINE (no return before :highlight)page.insert html :top, "my list", " li id '#{session[:comment list].length}' #{params[:newitem]} /li "page[session[:comment list].length.to s].visual effect:highlight, :startcolor "#ffff00", :endcolor "#ffffff"

Add MagicWhat have we got? Highlighting!

Done!Next class:Connecting to a databaseUsing migrationsScaffoldModel relationshipsUsing before filter

Ruby On Rails James Reynolds. Today Ruby on Rails introduction Run Enviornments MVC A little Ruby Exercises. Installation Mac OS X 10.5 will include Rails Mac OS X 10.4 includes Ruby . What happens if JavaScript is off? No JavaScript That is unacceptable! No JavaScript Change example_controller.rb

Related Documents:

Note from the Publisher The Ruby on Rails 3 Tutorial and Reference Collectionconsists of two bestselling Rails eBooks: Ruby on Rails 3 Tutorial:Learn Rails by Example by Michael Hartl The Rails 3 Wayby Obie Fernandez Ruby on Rails 3 Tutorial:Learn Rails by Exampleis a hands-on guide to the Rails 3 envi- ronment.Through detailed instruction,you develop your own complete sample

We are installing Ruby On Rails on Linux using rbenv. It is a lightweight Ruby Version Management Tool. The rbenv provides an easy installation procedure to manage various versions of Ruby, and a solid environment for developing Ruby on Rails applications. Follow the steps given below to install Ruby on Rails using rbenv tool.

Contributing to Ruby on Rails Rails is not 'somebody else's framework.' This guide covers a variety of ways that you can get involved in the ongoing development of Rails. API Documentation Guidelines This guide documents the Ruby on Rails API documentation guidelines. Ruby on Rails Guides Guidelines This guide documents the Ruby on Rails guides .

HP PHP PHP PHP PHP PHP HiPE Erlang HiPE Erlang HiPE . Perl Perl Perl Perl Perl Perl Ruby Ruby Ruby Ruby Ruby Python 3 Python 3 Python 3 Lua Lua Lua Lua Lua Lua Ruby Matz's Ruby Matz's Ruby benchmarks game 01 Mar 2019 u64q p r o . Python configures and steers fast C/C /Fortran code Passes memory buffers from one library to the next

What is Ruby on Rails Ruby on Rails is a web application framework written in Ruby. David Heinemeier Hansson developed it in 2003. Since then Improvised by Rails Core Team. Released in 2004 termed as RoR. Works on Windows, Macintosh, Linux. Compatible to all common databases. Page 2

WEEK 2 – Introduction to Ruby 2.0 Getting to Know Ruby 2.0.a Ruby Basics History o Invented by Yukihiro Matsumoto o Popularized by Ruby on Rails framework Dynamic, OO, Elegant, expressive, and declarative Designed to make programmers happy So like this is going to be iterating over something three times: o 3.times Ruby basics

A Beginner's Guide to Security with Ruby on Rails in BSD Corey Benninger. What's on Tap? Ruby on Rails BSD Security. . Openbsd 3.9 – Install Ruby (pkg_add ruby-1.8.4p1.tgz) . for more install details. Getting the Goods

building processes to facilitate group work. Do nothing, join in and comment on what’s going well. Experiment with group structures and explore process improvements. Help the group critique itself. Your role as leader becomes less active. Arrange appropriate ceremonies/rituals for celebration of accomplishments. Use or suggest inclusion activities that give new members a sense of acceptance .