<
Ruby Rails - First Exploration
>
上一篇

Digital Forensics
下一篇

Machine Learning

Rails - Ruby

Version:

Environment

  1. Install Rails on your computer

    # Firstly you should have installed the Ruby 2.6.5
    $ gem install rails -v 5.2.4
    $ rails -v #check your version
    
  2. Setting up the Sqlite3( The Sqlite 3 is the defalut database by Rtails)

    $ brew install sqlite3
    
  3. Install the Yarn and ChromeDriver

    $ brew install yarn
    $ brew cask install chromedriver
    

1. Hello Rails

1.1 Create Your First Rails Projects

# You should find a empty folder and change the current directory
# 
$ pwd
$ rails new testprojects
$ cd testproject
$ rails db:migrate
$ rails server
$ rails about # show the details of created Rails project
# $ bundle exec rails about
=> Booting Puma
=> Rails 5.2.4 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.2 (ruby 2.6.3-p62), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

helloRails

$ rails server -b 0.0.0.0
$ rails generate controller kernel hello goodbye
$ cd /app/controllers & ls 
application_controller.rb	kernel_controller.rb
concerns
$ cd .. && cd ./views/say && ls
goodbye.html.erb	hello.html.erb

kernel_controller.rb - the kermel module to control the view layer. The pages the rb file controls are in the path /views/kernel. And the ruby files have created two methods to pass the parameters.

hello.html.erb and goodbye.html.erb are created by the script and they are static page in Rails.

1.2 Pages

Static and Dynamic Pages

$ vim /app/views/kernel/hello.html.erb
<h1>Say#hello</h1>
<p>
    Find me in app/views/say/hello.html.erb
    The year is <%= @time.year %> # To insert the variables from the controller
</p>

$ vim /app/controller/kernel_controller.rb
class KernelController < ApplicationController
  def hello
    @time = Time.new
  end

  def goodbye
  end
end
# This example indicates the ways for users to control the website pages by ruby files.

2. First Application(Models,Views and Controllers)

Models : maintaining the state of the application. It can handle with the various interface and communication.

View : generate the user interface and is based on the data in the model.

Controller : orchestrate the application and schedule the process of the application.

Top
Foot