The Controller Carol Wolf Computer Science. Rails generate commands  Using the generate command, you can create a number of useful objects.  Rails:

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
Advertisements

The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
CS 142 Lecture Notes: Rails Controllers and ViewsSlide 1 Simple Rails Template
Introduction to MVC Adding a View Page NTPCUG Tom Perkins, Ph.D.
Ruby on Rails Model of MVC. Model-View-Controller Paradigm A way of organizing a software system Benefits: Isolation of business logic from the user interface.
Software to Manage EEP Vegetation Plot Data A design proposal Michael Lee January 31, 2011.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
Academic Year, Spring Semester Bilkent University - Faculty of Art, Design and Architecture Department of Communication and Design CS 153 Introduction.
XML and Ruby on Rails Jennifer Andrews LIS 531F April 25,2007.
15-Jun-15 Rails and Ajax. HTML Forms The... tag encloses form elements (and usually includes other HTML as well) The arguments to form tell what to do.
CSCI 4550/8556 Computer Networks Comer, Chapter 19: Binding Protocol Addresses (ARP)
Methods for Rails. File Structures This is taken directly from app Holds all the code that's specific.
Ruby on Rails Creating a Rails Application Carol E Wolf CS396X.
Ruby on Rails (Slides modified by ements-2ed.shtml)
Ruby on Rails: An Introduction JA-SIG Summer Conference 2007 Michael Irion The University of Tulsa.
Chapter 8 Printing 1. In COBOL you send data to the printer by writing data to a file. In COBOL, the printer is defined as a file, and it is opened, closed,
0 1 Presented by MANSOUREH SERATI Faculty Member of Islamic World Science Citation Center (ISC) shiraz, Iran.
J2EE Web Fundamentals Lesson 1 Introduction and Overview
Server-side Scripting Powering the webs favourite services.
Lecture 7 Interaction. Topics Implementing data flows An internet solution Transactions in MySQL 4-tier systems – business rule/presentation separation.
Web Application Programming Carol Wolf Computer Science.
Ajax and Ruby on Rails Session 9 INFM 603.
Views Carol Wolf Computer Science. Extended Ruby  Views files are written in extended Ruby, erb.  They end in.html.erb.  Ruby code is intermixed with.
1 Dr Alexiei Dingli Web Science Stream Advanced ROR.
Standalone Java Application vs. Java Web Application
Forms Carol Wolf Computer Science. The Controller  To create a controller, type  rails generate controller pizza index order  This creates a controller.
LLRP GUI Client User Guide
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
Customizing your own SENSORS (site) Ethan Danahy Tufts University June 7 th, 2001.
Sayed Ahmed Computer Engineering, BUET, Bangladesh MSc., Computer Science, Canada
Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for.
Introduction to Entity Framework Part 2 CRUD Scaffolding Tom Perkins NTPCUG.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Ruby on Rails CSE 190M, Spring 2009 Week 6. Overview How to use a database Demo creating a blog application on Rails Explain how the application works.
1 Basic Perl CGI Programming. 2 Issues How and when your program is invoked. Generating Response –HTTP Headers –HTML (or whatever document type you want)
1 Dr Alexiei Dingli Web Science Stream A ROR Blog.
ITCS373: Internet Technology Lecture 5: More HTML.
1 Dr Alexiei Dingli Web Science Stream A ROR Twitter.
Routes & REST & URL-helpers & validations & filters
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
Testing Carol Wolf Computer Science. Testing built into Rails  Rails comes with three databases.  development  test  production  The test database.
XDStarClient Presentation of a suite of tools developed by IHE Europe for healthcare community Abderrazek Boufahja Mai 25, 2012.
Web Center Training ©2003 Optimum Technology, Inc.
Chapter 15 © 2013 by Pearson Overview of Rails - Rails is a development framework for Web-based applications - Based on MVC architecture for applications.
Create, Update and Delete Carol Wolf Computer Science.
ICM – API Server & Forms Gary Ratcliffe.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
ICM – API Server Gary Ratcliffe. 2 Agenda Webinar Programme API Server Overview JSON-RPC iCM API Service API Server and Forms New services under.
CSC 2720 Building Web Applications Basic Frameworks for Building Dynamic Web Sites / Web Applications.
Adding Data to a Database Table Carol Wolf Computer Science.
CS 142 Lecture Notes: FormsSlide 1 Simple Form Product: Price:
Rails and routing INFO 2310: Topics in Web Design and Programming.
AJAX in Ruby-on-Rails. Ruby on Rails and AJAX AJAX can be done with just Javascript Easier if you use libraries –Prototype –SAJAX –jQuery Libraries only.
CS 160 and CMPE/SE 131 Software Engineering February 9 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Introduction to information systems RUBY ON RAILS dr inż. Tomasz Pieciukiewicz.
Migrations Carol Wolf CS 396X. ISBNTitleAuthorImage EmmaAustenemma.jpg Oliver TwistDickenstwist.jpg HamletShakespearehamlet.jpg.
AJAX CS456 Fall Examples Where is AJAX used? Why do we care?
CS 160 and CMPE/SE 131 Software Engineering February 11 Class Meeting Department of Computer Science Department of Computer Engineering San José State.
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.
1 Rails for the Ruby-Impaired John Paul Ashenfelter CTO/Transitionpoint.
Routes Carol Wolf Computer Science. RESTful Architecture  Rails uses REST-style architecture: representation state transfer  resources :courses in routes.rb.
Play Framework: Introduction
This shows the user interface and the SQL Select for a situation with two criteria in an AND relationship.
Test Case Structure Test Case Module(depend on framework) MocoServer
Build Better Apps with MEAN.
Bruce Scharlau, University of Aberdeen, 2017
Model – View – Controller Pattern
Chapter 15 Introduction to Rails.
CS 142 Lecture Notes: Rails Controllers and Views
Chengyu Sun California State University, Los Angeles
Presentation transcript:

The Controller Carol Wolf Computer Science

Rails generate commands  Using the generate command, you can create a number of useful objects.  Rails:  controller  generator  helper  integration_test  mailer  migration  model  observer  performance_test  plugin  resource scaffold  scaffold_controller  session_migration  stylesheets

Generating a controller  Use rails generate controller controller_name method1 method 2 …  The controller’s name will be the one you give it followed by _controller.  pizza_controller  schedule_controller  librarian_controller  The new controller will have the methods named in the generate command.  The views folder gets a new sub-folder with the name of the new controller. It will contain ERB files for each method.  The file, route.rb, will get a new route for each one of the methods. They will all be ‘gets’. Some will have to be changed to ‘posts’.

A new controller for the schedule application  The following will generate a new controller, views and routes: rails generate controller schedule index list_courses find_course  The controller will be called schedule_controller.  The methods are index, list_courses and find_course.  The views are index.html.erb, list_courses.html.erb and find_course.html.erb.  The new routes are  get "schedule/index "  get "schedule/list_courses "  get "schedule/find_course"  The first one can remain as a get, but the others must be post.

The directory structure

The routes file Testapp::Application.routes.draw do get "schedule/index" post "schedule/list_courses" post "schedule/find_course“  These changes are necessary in Rails. No parameters are sent when you access the index page. But they may be included with the other two. Whenever parameters may be used, Rails requires post.

Listing courses – controller method class ScheduleController < ApplicationController def index end def = Course. find(:all, :order => "number") end def find_course end  This will access the database table and return all its contents, ordered by the number field.

The index file  The index file has a form that sends control to the list_courses method in the controller, which will respond to the request. List all Courses {:action => :list_courses} do |form| %>  The controller responds by sending back list_courses.html.erb.

The list_courses.html.erb file Listing courses Number Name Credits

Index page code to find a single course Find a Course {:action => :find_course} do |form| %> Course Number: 20 %>

Index page

Controller code for finding a course def = = respond_to do |format| != nil format.html else format.html { render :action => "not_found" } end

Controller code  Parameters are sent to the controller from the form in a hash. Here the hash is  “course” => {“number” => “CS 121”}  We must first extract the hash. = params[:course]  And then we can use the keys to extract the values.  This is cumbersome, but it works.

Response page – find.course.html.erb Number: Name: Credits:

Response page

Not found page  Often when searching the user makes a mistake. In that case the course will not be found. A simple page can be added to the views/schedule folder to catch these errors.  not_found.html.erb The course was not found