Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby on Rails Kickstart 103 & 104

Ruby on Rails Kickstart 103 & 104

Ruby on Rails Kickstart 103 & 104.
Presentation file for NCCU Trend Tech Club.

Avatar for Heng-Yi Wu

Heng-Yi Wu

October 14, 2015

More Decks by Heng-Yi Wu

Other Decks in Programming

Transcript

  1. Getting started 1. Ruby on Rails command 2. Generate an

    article scaffold • MVC pattern 3. Synchornize database schema • ORM 4. Modify scaffold
  2. Ruby on Rails Command rails generate scaffold scaffold_name field1_name:field1_type \

    field2_name:field2_type ... fieldN_name:fieldN_type • Replace scaffold_name with actual scaffold names • Replace field1_name to fieldN_name with actual field names
  3. Ruby on Rails command (Cont.) field1_type to fieldN_type needs to

    be a valid data type source: https://ihower.tw/rails4/migrations.html#section
  4. Generate an article scaffold rails generate scaffold article title:string body:text

    The command generates... • a controller handles requests from clients and transfer data between models and views • a model with one string field called title and one text field body called article • a form for create and update articles and several views to list articles and to display single article
  5. Synchronize database schema rake db:migrate • rake runs scripts called

    Rakefile. Rakefile holds a bundle of commands related to Ruby on Rails but not part of Ruby on Rails • db:migrate: synchronize database schema, as known as database migration • tmp:clear: clean temporary files
  6. Why bothered? In PHP $title = mysql_real_escape_string($_POST['title']); $body = mysql_real_escape_string($_POST['body']);

    $sql = "INSERT INTO `articles` (`title`, `body`)" . " VALUES ('{$title}', '{$body}')"; mysql_query($sql);
  7. Don't bother In Ruby on Rails # params[:article] = {

    # title: 'Article title', # body: 'Article body' # } article = Article.new(params[:article]) article.save ORM saves the day
  8. ORM (Cont.) • Object Relational Mapping • Object stands for

    objects in Ruby on Rails • Relational stands for relational database system, such as MySQL, PostgreSQL, Microsoft SQL Server...etc. • Mapping stands for the procedure transfer data structure into table row
  9. Start web server If you forget how to do so,

    feel free to take a look on #101
  10. Add a field 1. Modify model 1. Create a database

    migration 2. Synchronize database schema 2. Modify the controller 3. Modify views
  11. Create a database migration rails generate migration \ add_author_to_articles author:string

    • Replace author with field name you want to add • Replace articles with plural form of model • Replace string with valid data type from the table mentioned before
  12. Modify the controller # Only allow a trusted parameter "white

    list" through. def article_params params.require(:article).permit(:title, :body, :author) end • Append :author to the list of white list
  13. show view ... </p> <p> <strong>Author:</strong> <%= @article.author %> </p>

    <%= link_to 'Edit', edit_article_path(@article) %> | ...
  14. Remove a field from scaffold 1. Create a database migration

    2. Synchronize database schema 3. Modify the controller 4. Modify views
  15. Controller (Cont.) 1. One controller has many actions 2. Each

    action has its own purpose 3. Actions are isolated to each other 4. One action takes care of one request
  16. Model (Cont.) 1. Model DOES NOT hold fields, schema DOES

    2. Model is a class 3. In ORM, fields would be mapped as properties in object, thus we can manipulate them via methods
  17. Model (Cont.) • HumanBeing : Model • Person : Object

    • People : Iterable object (a.k.a. array) of objects
  18. Model (Cont.) @all_people = HumanBeing.all @adults = HumanBeing.where('age >= ?',

    20) @person = HumanBeing.find_by(identity: 'A123456789')
  19. Views (Cont.) 1. Symbol @ points to corresponding controller in

    views 2. DO NOT conduct complicated calculations or property access in views 3. Views should have only if and each statements
  20. Homework 1. Create a scaffold from scratch 2. Add a

    field to the scaffold 3. Remove a existing field from the scaffold