Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ruby on Rails. Web Framework for Ruby Designed to make it easier to develop, deploy, and maintain web applications Design with Model-View-Controller –almost.

Similar presentations


Presentation on theme: "Ruby on Rails. Web Framework for Ruby Designed to make it easier to develop, deploy, and maintain web applications Design with Model-View-Controller –almost."— Presentation transcript:

1 Ruby on Rails

2 Web Framework for Ruby Designed to make it easier to develop, deploy, and maintain web applications Design with Model-View-Controller –almost forced Also based on DRY concept –Don’t Repeat Yourself (DRY) –Every piece of knowledge should be expressed in just one place

3 MVC

4 Rails and MVC Incoming requests are first sent to a router. Router determines –where in the application the request should be sent and –how the request itself should be parsed. Router identifies a particular method in the controller code. –called an action The action may –look at data in the request, –interact with the model, –cause other actions to be invoked. The action prepares information for the view, which renders something to the user.

5 Rails and MVC

6 2 Main components in Rails –Action Pack and Active Record Active Record –Model support –Simplifies database interaction –Addressed in another lecture Action Pack –Controller and View are tightly coupled –Single Rails component –View code and Controller code are separate

7 View Support View is responsible for creating all or part of a page Can include dynamic content.html.erb files –may contain embedded code much like php, jsp –developers must be careful to maintain clean separation of Model, View, and Controll code Rails also supports the creation of page templates and helpers –helpers are methods that make it easier to write page templates

8 The Controller Logical center of your application The controller is a class that is initially generated by rails –methods map to actions and are logically connected to views Controller class variables are available to views –Controller sets the variable –Views then use the variable Also responsible for routing, caching, and session management

9 Creating a Ruby-on-Rails Project create a projects directory rails my_app

10 More Rails Rails has scripts to help you –run the scripts –customize files to fit your needs Rails goal –Always have something working –Incremental development –Agile

11 Hello World rails HelloWorld ruby script/generate controller Say Look at controller –app/controllers/say_controller.rb –pretty minimal Add an action and a corresponding view class SayController < ApplicationController end

12 HelloWorld/ app/ controllers/ say_controller.rb models/ views/ say/ hello.html.erb class SayController < ApplicationController def hello end Hello World Hello World http://localhost:3000/say/hello

13 Rails naming conventions Controller Naming –URLhttp://……/say/hello –Fileapp/controllers/say_controller.rb –ClassSayController –Methodhello –Layoutapp/views/layouts/say.html.erb View Naming –URLhttp://……/say/hello –Fileapp/views/say/hello.html.erb –Helpermodule SayHelper –Fileapp/helpers/say_helper.rb

14 Rails Dynamic Content We can embed dynamic content in.html.erb files –embedded ruby code We may embed –expressions to be evaluated –ruby code : Hello putting the - sign(-%>) at the end eliminates newlines for the ruby code lines in the generated html

15 Rails Dynamic Content We may also embed controller instance variables class SayController < ApplicationController def hello @time = Time.now end Hello World Hello World It is now

16 Rails linking Add another action and view - goodbye class SayController < ApplicationController def hello @time = Time.now end def goodbye end Goodbye World Goodbye World

17 Rails Page Linking Could simply add anchor – Goodbye Fragile –Could move –Too much reliance on rails url format Use link_to – “goodbye” %>

18 Form Data Form data is available in the params hash –Holds all the data passed in a browser request name = params[“name”] age = params[“age”] age = params[:age] –remember :age maps to “age” Available whether using GET or POST

19 Sessions Ruby manages sessions for you. session hash –session[“loggedin”] = true –session[“loginrole”] = “admin” To reset the session use –reset_session

20 Having some fun List files in a directory –Add index action to say_controller.rb def index @files = Dir.glob(‘*’) end –Add the index view file – index.html.erb file:

21 Helpers A module containing methods to help a view –each controller gets its own helper Use to reduce the amount of code needed by view module SayHelper def file_list list = ' ' for file in Dir.glob('*') do list += ' ' + file + "\n" end list += ' ' end Index File listing

22 Other Helpers Rails comes with a bunch of helpers – 62 days – $123.45 – 212-555-1212 – 12,345,678 Many others - look at the Action View RDoc

23 Layouts Templates for views –app/views/layouts One per view or application –viewname.html.erb –application.html.erb Specify look and feel. Eliminate those portions from corresponding views. –only need the partial.html.erb code in view

24 Layouts Hello & Goodbye World Goodbye World "hello" %> app/views/layouts/application.html.erb app/views/say/goodbye.html.erb

25 Default Page By default, whatever doesn’t match to a controller and action goes to the public directory Can change default page to a controller and action –edit config/routes.rb –remember to remove public/index.html # You can have the root of your site routed by hooking up '' # -- just remember to delete public/index.html. # map.connect '', :controller => "welcome" map.connect '', :controller => 'say', :action => 'hello'


Download ppt "Ruby on Rails. Web Framework for Ruby Designed to make it easier to develop, deploy, and maintain web applications Design with Model-View-Controller –almost."

Similar presentations


Ads by Google