BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Microblog App I 2020

RubyOnRails_logo




Bookmark and Share





bogotobogo.com site search:





Note

Let's check the versions of ruby and rails:

$ ruby -v
ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]

$ rails -v
Rails 4.2.5.1




microblog app

Almost all Rails applications start by running rails new command. We generate a skeleton Rails application in a directory we choose.

$ cd ~/rails_projects
$ 
$ rails new microblog_app
$ cd microblog_app

As we can see rails new automatically runs the bundle install command after the file creation is done. Notice that the rails command creates numerous files and directories.





data model

Typically when we make a web application, first thing we do is creating a data model. The data model is a representation of the structures needed by our application, microblog.

We'll begin with a model for users of the app, and then we'll add a model for microposts.





data model - users

Users of our microblog app will have a unique integer identifier called id, a publicly viewable name (of type string), and an email address (also a string) that will be used as a username.

Here is the users table:

users
id integer
name string
email string

For the users table in database, the id, name, and email attributes are columns in that table.





data model - microposts

Our micropost has only an id and a content field for the micropost's text (of type string). We also want to associate each micropost with a particular user by recording the user_id of the owner of the post.

Here is the micropost table:

microposts
id integer
content string
user_id integer





Resource - rails generate command

We're going to implement the users data model defined in the earlier section.

The Users resource will allow us to think of users as objects that can be created, read, updated, and deleted (CRUD) through the web via the HTTP protocol. Our Users resource will be created by a scaffold generator which comes standard with each Rails project.

By passing the scaffold command to the rails generate script, we can generate Rails scaffolding. The argument of the scaffold command is the singular form of the resource name, User, together with optional parameters for the data model's attributes.

$ rails generate scaffold User name:string email:string
      invoke  active_record
      create    db/migrate/20140519174512_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
      invoke  resource_route
       route    resources :users
      invoke  scaffold_controller
      create    app/controllers/users_controller.rb
      invoke    erb
      create      app/views/users
      create      app/views/users/index.html.erb
      create      app/views/users/edit.html.erb
      create      app/views/users/show.html.erb
      create      app/views/users/new.html.erb
      create      app/views/users/_form.html.erb
      invoke    test_unit
      create      test/controllers/users_controller_test.rb
      invoke    helper
      create      app/helpers/users_helper.rb
      invoke      test_unit
      create        test/helpers/users_helper_test.rb
      invoke    jbuilder
      create      app/views/users/index.json.jbuilder
      create      app/views/users/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/users.js.coffee
      invoke    scss
      create      app/assets/stylesheets/users.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss

Now, the User model has a form shown in the table above by including name:string and email:string.

The the primary key id in the table is created automatically by Rails.





Resource - migration using rake

We need to migrate the database using rake.

We simply update the database with our new users data model:

$ bundle exec rake db:migrate
== 20160127231829 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0125s
== 20160127231829 CreateUsers: migrated (0.0164s) =============================

Note that we needed to run rake using bundle exec in order to ensure that the command uses the version of Rake corresponding to our Gemfile.





Resource - running server

Now, we can run the local web server using rails server:

$ rails server
=> Booting WEBrick
=> Rails 4.2.5.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-01-27 15:23:17] INFO  WEBrick 1.3.1
[2016-01-27 15:23:17] INFO  ruby 2.1.3 (2014-09-19) [x86_64-linux]
[2016-01-27 15:23:17] INFO  WEBrick::HTTPServer#start: pid=14445 port=3000




Resource - http://localhost:3000/

Now our application is ready for http://localhost:3000/.

localhost3000.png

This root url http://localhost:3000/ page is the same default Rails page we've seen in previous chapter.

Note that we also created a large number of pages for manipulating users.

For example, the page for listing all users is at /users, and the page for making a new user is at /users/new.

Users_Browser.png


New User_Browser.png






Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Ruby on Rails



Ruby On Rails Home

Ruby - Input/Output, Objects, Load

Ruby - Condition (if), Operators (comparison/logical) & case statement

Ruby - loop, while, until, for, each, (..)

Ruby - Functions

Ruby - Exceptions (raise/rescue)

Ruby - Strings (single quote vs double quote, multiline string - EOM, concatenation, substring, include, index, strip, justification, chop, chomp, split)

Ruby - Class and Instance Variables

Ruby - Class and Instance Variables II

Ruby - Modules

Ruby - Iterator : each

Ruby - Symbols (:)

Ruby - Hashes (aka associative arrays, maps, or dictionaries)

Ruby - Arrays

Ruby - Enumerables

Ruby - Filess

Ruby - code blocks and yield

Rails - Embedded Ruby (ERb) and Rails html

Rails - Partial template

Rails - HTML Helpers (link_to, imag_tag, and form_for)

Layouts and Rendering I - yield, content_for, content_for?

Layouts and Rendering II - asset tag helpers, stylesheet_link_tag, javascript_include_tag

Rails Project

Rails - Hello World

Rails - MVC and ActionController

Rails - Parameters (hash, array, JSON, routing, and strong parameter)

Filters and controller actions - before_action, skip_before_action

The simplest app - Rails default page on a Shared Host

Redmine Install on a Shared Host

Git and BitBucket

Deploying Rails 4 to Heroku

Scaffold: A quickest way of building a blog with posts and comments

Databases and migration

Active Record

Microblog 1

Microblog 2

Microblog 3 (Users resource)

Microblog 4 (Microposts resource I)

Microblog 5 (Microposts resource II)

Simple_app I - rails html pages

Simple_app II - TDD (Home/Help page)

Simple_app III - TDD (About page)

Simple_app IV - TDD (Dynamic Pages)

Simple_app V - TDD (Dynamic Pages - Embedded Ruby)

Simple_app VI - TDD (Dynamic Pages - Embedded Ruby, Layouts)

App : Facebook and Twitter Authentication using Omniauth oauth2

Authentication and sending confirmation email using Devise

Adding custom fields to Devise User model and Customization

Devise Customization 2. views/users

Rails Heroku Deploy - Authentication and sending confirmation email using Devise

Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger I

Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger II

OOPS! Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger (Trouble shooting)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master