只是怕忘了命令,全部撸一次,记个大概。。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
安装Ruby之前,先要安装RVM:
curl -L https://get.rvm.io | bash –s
然后,安装一些依赖之后:
rvm install ruby version --with-openssl-dir=$HOME/.rvm/usr
之后安装Rails:
gem install rails --version 4.0.0 --no-ri --no-rdoc
创建Rails新程序:
rails new first_app
启动Rails测试服务器:
rails server –b 0.0.0.0
Rails产生静态资源文件:
rails generate controller StaticPages home help --no-test-framework
手动创造Rails之后,默认路由不是以资源方式定义的:
SampleApp: :Application. routes. draw do
root to: 'static_pages#home'
resources :users
resources :sessions, only: [:new, :create, :destroy]
match '/signup', to: 'users#new', via: 'get'
match '/help' , to: 'static_pages#help' , via: 'get'
end
新建的控制器内容都是空的:
class StaticPagesController < ApplicationController
def home
end
def help
end
end
其生成的视图也只是一个提示性的HTML:
<h1>StaticPages#home</h1>
<p>Find me in app/views/static_pages/home.html.erb</p>
Rails提供了特别的布局文件application.html.erb,可以提供网页相同的结构:
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield( :title) %></title>
<%= stylesheet_link_tag "application" , media: "all" ,
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application" , "data-turbolinks-track" => true %>
<%= render 'layouts/shim’>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash. each do | key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug( params) if Rails. env. development? %>
</div></body>
</html>
Rails除了提供内置方法供我们在视图中使用,我们还可以自建帮助方法,在视图中使用。
app/helper/static_pages_helper.rb
app/helper/application_helper.rb
Rails中创建LINK的链接及图片:
<%= link_to "Sign in" , '#' %> 第一个是链接文本,第二个是链接地址
<%= link_to "About" , about_path %>
<%= link_to image_tag( "rails.png" , alt: "Rails" ), 'http://rubyonrails.org/' %>
asset pipeline的目录及自定义样式文件:
app/assets/stylesheets
app/assets/stylesheets/custom.css.scss
静态资源目录:
• app/assets
• lib/assets
• vendor/assets
局部视图:
<%= render 'layouts/shim' %>
app/views/layouts/_shim.html.erb
生成用户模型:
rails generate model User name:string email:string
进行数据迁移:
bundle exec rake db:migrate
入库前进行数据有效性验证(电邮不重复,密码加密码,长度不小于3,用户名不超过50)
class User < ActiveRecord::Base
before_save {self.email = email.downcase }
validates :name, presence: true, length: {maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 3 }
end
确保电邮唯一性的方法:
生成索引:
rails generate migration add_index_to_users_email
修改迁移文件:
class AddIndexToUsersEmail < ActiveRecord: :Migration
def change
add_index :users, :email, unique: true
end
end
安全密码实现过程:
GEM需求
gem 'bcrypt-ruby' , '3.0.1'
bundle install.
数据库增加新列:
rails generate migration add_password_digest_to_users password_digest:string
bundle exec rake db:migrate
用户注册表单:
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :姓名 %>
<%= f.text_field :name %>
<%= f.label :邮箱 %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
资源模型的控制器标准操作:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
自定义出错信息,放在app/views/shared/_error_messages.html.erb
<% if @user. errors. any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize( @user. errors. count, "error" ) %>.
</div>
<ul>
<% @user. errors. full_messages. each do | msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
生成session控制器语法
rails generate controller Sessions --no-test-framework
登陆时的FORM
<%= form_for( :session, url: sessions_path) do | f| %>
<%= f. label :email %>
<%= f. text_field :email %>
<%= f. label :password %>
<%= f. password_field :password %>
<%= f. submit "Sign in" , class: "btn btn-large btn-primary" %>
<% end %>
用户包含sessionHelper(正常时只能VIEW用,而CONTROLLER用要明文INCLUE)
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
include SessionsHelper
end
为表增加新字段,且为此字段增加索引:
rails generate migration add_remember_token_to_users
db/migrate/[timestamp]_add_remember_token_to_users.rb
class AddRememberTokenToUsers < ActiveRecord: :Migration
def change
add_column :users, :remember_token, :string(要自己加哟)
add_index :users, :remember_token(要自己加哟)
end
end
生成token的回调函数:
class User < ActiveRecord: :Base
before_save { self. email = email. downcase }
before_create :create_remember_token
.
def User. new_remember_token
SecureRandom. urlsafe_base64
end
def User. encrypt( token)
282
Digest: :SHA1. hexdigest( token. to_s)
end
private
def create_remember_token
self. remember_token = User. encrypt( User. new_remember_token)
end
end
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "Autop", '#', id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", '#' %></li>
//<li id="fat-menu" class="dropdown">
//<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Accout <b class="caret"></b>
</a>
//<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"> </li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
//</ul>
//</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>