This README would normally document whatever steps are necessary to get the application up and running.
Things you may want to cover:
-
Ruby version
-
System dependencies
-
Configuration
-
Database creation
-
Database initialization
-
How to run the test suite
-
Services (job queues, cache servers, search engines, etc.)
-
Deployment instructions
-
...
It should echo nothing when everything goes well;
rails db:schema:dump
rails generate controller demo index
Default routes will no longer be supported.
get ':controller(/:action(/:id))'
root 'demo#index'
get 'demo/index'
Vallina Ruby statements.
<% %>
Erb file and it's template grammer.
<%= "bar #{foo}" %>
Double quotes are not supported in Ruby's template. (.erb file)
Conversation with controller.
// controller
@foo = "bar"
// erb
<%= "#{@foo}" %>
Erb template provides a built-in function--link_to for links.
<a href="/demo/index">index</a> // not recommend
<%= link_to(text, target) %>
<%= link_to("Back To Index", {controller: 'demo', action: "index"}) %>
if two pages are in the same controller, you needn't to pass the controller param;
Link
// Url varies depends on your routes setting.
// Dynamic links to pass params to controllers
<%= link_to('hello', {:controller => 'demo', :action => 'index', :id => 55}) %>
There are two ways to get params from url.
// controller
@id = params['id']
@name = params[:name]
// view
<%= @id %>
- Aim to ctrl database;
- Written by Ruby;
rails generate migration DoTestDemo
Migration name must use camelcase.
Generate a model.
rails g model User name:string email:string
Create mysql table;
rails db:migrate
Rollback;
rails db:rollback
Get into sandbox mode.
rails console --sandbox
Create users in sandbox mode.
ntnyq = User.new(name: "ntnyq", email: "me@ntnyq.com")
// Need validation pattern set.
ntnyq.valid?
ntnyq.save
// new and save in one step
goy = User.create(name: "goy", email: "goy@ntnyq.com")
// list all users
User.all
// show first user
User.first
User.find(id)
User.find_by(name: "goy")
goy.name = "gay"
goy.save
goy.update_attribute(:name, "goy")
goy.delete
See more At Rails Validation
validates :name, { presence: true, length: {minimum: 5, maximum: 20} }
VALD_EMAIL_REGEXP = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 20 },
format: { width: VALD_EMAIL_REGEXP },
uniqueness: { case_sensitive: false }
Mysql suggest you using capitalize letters for it's key words.
DESCRIBE users;
Create a branch and set it currently.
git checkout -b branch_name
git checkout master
git checkout -