Download presentation
Presentation is loading. Please wait.
1
Methods for Rails
2
File Structures This is taken directly from http://api.rubyonrails.com/http://api.rubyonrails.com/ app Holds all the code that's specific to this particular application. app/controllers Holds controllers that should be named like weblog_controller.rb for automated URL mapping. All controllers should descend from ActionController::Base. app/models Holds models that should be named like post.rb. Most models will descend from ActiveRecord::Base. app/views Holds the template files for the view that should be named like weblog/index.rhtml for the WeblogController#index action. All views use eRuby syntax. This directory can also be used to keep stylesheets, images, and so on that can be symlinked to public. app/helpers Holds view helpers that should be named like weblog_helper.rb.
3
Typical flow of control A request from the browser goes to a controller in app/controllers The controller talks to the model in app/models (initially, by means of a scaffold) The model sends requests to the database (in db ) The database returns information to the model The model manipulates the information and returns it to the controller The controller uses a.rhtml template in app/views to send a response to the browser
4
The controller A request from the browser goes to a controller Create the controller with ruby script/generate controller ClassName (use \ instead of / on Windows) The controller will be app/controllers/class_name_controller.rb The controller extends ActionController::Base Make the browser request with http://host:port/ClassName/method http://host:port/ClassName/method If method is omitted, the default is index method must be defined in class_name_controller.rb
5
The model If you are going to use a database (most Rails applications do), create the database first Create the model with ruby script/generate model ClassName The model extends ActiveRecord::Base Inside the generated class, add the line scaffold :table where table is the name of a table in the database
6
Scaffolding The scaffold method is in ActionController::Scaffolding::ClassMethods scaffold adds these methods: index, list, show, destroy, new, create, edit, update
7
The view(s) Views are written as HTML templates, in files with the.rhtml extension The RHTML file uses ERb, embedded Ruby: Ruby statements (code to be executed, but not included in the HTML page) are enclosed in Ruby values, to be included in the resultant HTML, are enclosed in Some available methods are: start_form_tag, submit_tag, render, link_to, and end_form_tag
8
Where methods come from Controllers extend ActionController::Base Models extend ActiveRecord::Base All the relevant methods should be documented somewhere in http://api.rubyonrails.com/http://api.rubyonrails.com/ Modules (mixins) make this more complicated, because a class can “mix in” methods from a variety of modules For example, validates_numericality_of is in module ActiveRecord::Validations::ClassMethods
9
The End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.