First exploration: Ruby on rails

One. Installation

Ruby installation: https://ruby-china.org/wiki/rvm-guide

Note: After installing RVM and Gem< /p>

Install rails:

gem install rails
-v 5.1.4 # specific version installation span>

2. Create a Rails project

rails _5.1.4_ new hello_app # Create a project with the specified rails version

1. Project directory

Share a picture

2. About Explanation of catalog files

Share pictures

3. About Introduction to the version of the dependent package in the Gemfile in the project

(1). If the version number is not specified in the gem command, Bundler will automatically install the latest version. For example:

share picture

(2).Always Install the latest version (>=). For example:

share picture

(3). Only Install the version with the last number change (~>). For example:

share picture

Note: Ruby is a little strict on version control, which is very different from Python. Different versions may cause program errors.

4. Installation and update These gems

bundle install # install, Usually you have already installed it when you create the project

bundle update # update

5. Run the Rails project

rails server

share picture

3. Rails architecture pattern MVC

In the initial stage, let’s take a look The way the Rails application works will help somewhat. You may have noticed that there is a directory named app/ in the standard file structure of Rails applications, which has three subdirectories: models, views and con-trollers. This shows that Rails has adopted a “model-view-controller” (MVC for short) architecture pattern. This mode separates the data (such as user information) in the application from the code that displays the data. This is a commonly used architecture for the Graphical User Interface (GUI).

share picture

Note: The difference from Python’s Django framework is that Django’s architecture model is: MTV

4. The origin of everything — hello world!

Enter the file: hello_app/app/controllers/application_controller.rb

class ApplicationController < ActionController::Base


protect_from_forgery with: :exception

def hello
render html:
"hello, world"
end

end

Enter the file: hello_app/config/routes.rb

Rails.application.routes.draw do

# For details on the DSL available within this file, see http: //guides.rubyonrails.org/routing.html

root
"application#hello"

end

Then:

Share pictures

Note: hello, world is finally out, celebrate!!!

5. The whole test project

1 .First the entire database postgresql

Add something to the Gemfile file

group :production do

gem
'pg', '0.20.0'
end

2. Run bundle

bundle install --without production

< /div>

Note: When installing gems, specify the –without production option, do not install the gems used in the production environment

3 .Think about the data model in the library

share pictureshare picture

( 1). Let’s first generate the resources of Users

rails generate scaffold User name:string email:string

Note: Use scaffolding to generate CURD code, I advise you not to read it carefully (after all, you can’t understand it, right). To explain: Pass scaffold to rails generate command to use Rails scaffolding. The parameter passed to scaffold is the singular form of the resource name (User ), followed by some optional parameters to specify the fields in the data model

share picture

Note: Added name:string and email: in the executed command string, so that the User model in the figure can be realized. Note that there is no need to specify the id field, Rails will automatically create it and set it as the primary key of the table.

(2). The next step is to migrate the database

rails db:migrate

Share a picture

Note: It’s done~..~

Attention, Note: In versions prior to Rails 5, the db:migrate command was executed using rake instead of rails. Therefore, if you want to maintain the previous application, you must know how to use Rake

(3).User’s access path

share picture

Note: Go and see for yourself

(4). Before creating the second model , Try MVC

share picture Share a picture

①. Enter the file: hello_app/config/routes.rb

h4>

You will find that there are more lines in the file

share picture< /p>

Note: Rails routing defines a rule for the Users resource (The writing of: users looks very strange, it is a symbol, I will elaborate on it later)

Since it’s all here Just change the follow route to the index route of users, which is the list of users

share picture

②. Enter the file: hello_app/app/controllers/users_controller.rb

share picture

Note: The wording of class UsersController

Look at the code generated by the scaffolding for you, are you happy?

③. Enter the file: hello_app/app/views/users

Share a picture

Note: Take a look at the template generated by the scaffolding, and you will be happier after reading it Na

④. Insufficiency of insufficient code generated by scaffolding

• No verification data

There is no verification data. The User model will accept empty names and invalid email addresses without reporting an error.
• No authentication
No identity verification. Without the login and logout functions, any user can perform any operation.
• No test
No test. It's not completely absent. The scaffolding will generate some basic tests, but it is very rough and inconvenient. There are no tests for data verification and identity verification, let alone tests for other functions.
• No style, no layout
No style, no layout. There is no shared style and site navigation.
• Didn’t really understand
Isn’t it real.

(5). Generate Microposts resources

#  The old look

rails generate scaffold Micropost content:text user_id:integer

share picture

Note: For the rest of the database migration or something, turn it up by yourself, it’s too much trouble not to write

(6 ).Route generated by Microposts

share picture

Note : Try it yourself, and then look for other files created by the scaffolding, not to mention that you can’t find it, it says

. . .

Go here first, continue tomorrow

Install rails:

gem install rails
-v 5.1.4 # specific version installation span>

rails _5.1.4_ new hello_app # Create a project with the specified rails version

bundle install # Installation, usually when you create the project, it is already installed for you

bundle update # update

rails server

class ApplicationController < ActionController::Base


protect_from_forgery with: :exception

def hello
render html:
"hello, world"
end

end

Rails.application.routes.draw do

# For details on the DSL available within this file, see http: //guides.rubyonrails.org/routing.html

root
"application#hello"

end

group :production do

gem
'pg', '0.20.0'
end

bundle install --without production

rails generate scaffold User name:string email:string

rails db:migrate

• No verification data

There is no verification data. The User model will accept empty names and invalid email addresses without reporting an error.
• No authentication
No identity verification. Without the login and logout functions, any user can perform any operation.
• No test
No test. It is not completely absent. The scaffolding will generate some basic tests, but it is very rough and inconvenient. There are no tests for data verification and identity verification, let alone tests for other functions.
• No style, no layout
No style, no layout. There is no shared style and site navigation.
• Didn’t really understand
Isn’t it real.

# the old look< /span>

rails generate scaffold Micropost content:text user_id:integer

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3365 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.