This guide is positioned as a guide to the rails official guide and is written for beginners. This book provides an overview. Please refer to the official guide for details.
Rails 4.2
Ruby 2.0
You can try "one-hour warm-up" first. If you are interested, you can continue to study the basics.
Maybe you care how much time you need to learn. I give a reference time. Please pay attention to the reference time that I am talking about. It is enough to get started in one hour. If you are studying a complete project, it will take about two months. The accumulated three projects are six months. Can reach the intermediate level.
In addition, you can refer to the rich free introductory course Rails 101 of Taiwanese users xdite.
The installation of rails was very simple, but the domestic network environment made rails development more difficult. At present, there seems to be no good way.
start installation
Assuming ruby is already installed (recommended rvm installation management ruby, unfamiliar, you can go to the ruby-china forum wiki), the basic installation is very simple
#for Chinese, Configuration for Taobaoyuan
#bundle config 'mirror.https://rubygems.org' 'https://ruby.taobao.org'
#install rails
gem install rails -V
#-V shows details for slow net
Next generate a simple project
rails new rails-rsg
Next, all the used gems are listed in the bundle, /Gemfile, and the bundle is installed once.
cd rails-rsg
bundle install -V #-V shows the details, you can not
If it goes well, it will be installed in a few minutes. If it doesn't work, you may need to change the installation source of Taobao, or vpn.
The purpose of this warm-up experiment is to provide some perceptual knowledge. Most of the students do not like to read the theory.
Generate a simple blog post.
rails g scaffold Blog title:string body:text
The above command will generate some programs to create a data table, which will be explained in detail in the subsequent database section.
Next, execute the generated build data table program
rake db:migrate
If it is successful, the data table will be built. The default is to use sqlite3 database, which is very simple and practical.
The last step is to start the server.
rails s
The browser is ready to access.
http://localhost:3000/blogs
You can use the browser to operate, there are some perceptual knowledge.
How does the browser input URL correspond to the program? This is called routing. Let's take a look at the standard routing. If you haven't done web development, you may not understand it at first. There is an impression. Prefix is The routing name prefix is used in subsequent views, such as blogs_path.
The routing part that follows will learn further.
There are two main ones:
- Do not repeat the principle DRY
- Conventions are better than configuration principles CoC
Not repeating is the meaning of simplicity, and DSL (domain-specific language) is designed for different tasks. Conventional principles can ease the burden on the programmer.
As you learn more, you can appreciate these two principles.
/Gemfile
/config/database.yml
/config/routes.rb
The name can be explained, it will not be explained in detail. The route in the warm-up is generated by routes.rb.
resources :blogs
This line produces seven standard routes, which are very concise, which is why rails is called web DSL.
Referring to the map of the warm-up experiment, I used the blog name as an example. It is the get method, and the uri is written as /blogs/1.html. :id, :format is the notation for ruby symbols. (.:format) means that this part can be omitted, and finally corresponds to the method show of the controller BlogsController.
The basic route is seven, and the update has two, a total of eight.
More complicated, there are nested routes, as well as member, collection routes. Not complicated, just check it and you will know.
Rails c
Rails.application.routes.recognize_path "http://localhost:3000/users/11"
You can verify any route and which controller it corresponds to.
raise @order.inspect
This method is very convenient and intuitive, the browser can be seen, basically do not need to look at the terminal's less friendly black screen.
Gem better_errors can improve the default error prompt
Gem byebug can set breakpoints
These are the most commonly used database-related rake commands.
rake db:drop:all
rake db:create
rake db:migrate
rake db:seed
When you need to modify the data table, use the following command to generate a file.
rails g migration NameOfAny
After writing the code in the file (please refer to the specific method), then run it.
rake db:migrate
Mysql database requires gem mysql2
It is said that rails 5 will no longer use rake.
The following command lists all tasks
rake -T
You can write your own task, plus desc "description" before the task, it will appear in the task list.
Immediately after the routing is the controller. Can be generated with the command:
rails g controller Samples
Command generation model
rails g model Sample title:string
This will generate a data table migration file at the same time, please refer to the database section.
There are two choices, Capistrano and Mina, I have used the former. The latter is said to be simpler. Deployment can complete some automatic tasks, such as database modification, this is the ftp method that php can't do.
Deployment is a relatively complex topic, and it won't go into depth here.
A good introduction to gem from prograils, English version
- pry
Pry has many advantages, such as color, the utility command ls String and so on.
Can be used separately, familiar can be added to the rails, you need to add pry, pry-rails two gem in the Gemfile.
- postman
- Mysql Admin
I think the relationship between routing and model is two difficult points, so I can focus on learning.
Routing is relatively easy to experiment with the effects of different writing methods. The relationship of model includes has_one, has_many, belongs_to, etc. Please refer to the official guide. You can experiment in the rails console.
The view I found it difficult at first, but later I found out that the route was not understood clearly.
You can look at the files in the /app/views directory and understand that route.
For example, blogs_path, this method is automatically generated according to the route, there is also a blogs_url, the difference is that the url method has the http: protocol.
This is mainly to solve the problem of slow references to js, css, etc., and multiple files are combined into a single file. However, it also brings new complexity problems while solving.
In the development phase, directly reference /app/assets, if the debug is opened, the page will reference the independent file. In the deployment phase, you need to pre-compile with rake assets:precompile and generate it to public.
Gem quiet_assets can close the related display on the terminal.
Mobile phone development does not require a view, but it is simple.
This is fine.
render json: { blogs: @blogs }
I am currently writing with markdown, which is more flexible, but the effect is average. There are some popular programs that are not currently used.