Ruby On Rails 学习笔记
- 安装rails
- 安装RVM 及Ruby:
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3- 安装RVM及Ruby(参考):
\curl -sSL https://get.rvm.io | bash -s stable --ruby - 将下面这段加入
~\.bashrc,然后source ~\.bashrc。参考[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
- 安装nodejs:参考
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - sudo apt-get install -y nodejs - 安装Rails:
gem install rails --version 4.0.0 --no-ri --no-rdoc
- 安装RVM 及Ruby:
- 制作rails站点:
rails new <project_name>- 修改Gemfile
- 安装Gems:
bundle install、bundle update - [可选、一步到位]展开脚手架:
rails generate scaffold <ModelName> <field>:<type> <field>:<type>... - [或者、手动创建]创建控制器:
rails generate controller <ControllerName> <action> <action>... - 修改路由:
/config/routes.rb - 修改页面:
- 布局面
/app/views/layouts/application.html.erb - 控制器页面
/app/views/<controller_name>/<action>.html.erb - 局部视图
/app/views/layouts/_<partial_name>.html.erb
- 布局面
- 创建模型:
rails generate model <Model> <field>:<type> <field>:<type>... - 修改迁移文件
/db/migrate/[timestamp]_create_<model>s.rb - 迁移数据库
rake db:migrate - 尝试沙箱控制台
rails console --sandbox - 针对模型增加验证
/app/models/<model>.rb - 对DB建立索引
rails generate migration add_index_to_<model>s_<field>,然后修改/db/migrate/[timestamp]_add_index_to_<model>s_<field>.rb - 迁移数据库
rake db:migrate - [看情况]为用户模型增加密码验证等一套功能,在
/app/models/users.rb中加入has_secure_password - 表单使用
form_for(@model) do |f| ... end方法,提交错误可以使用@model.errors.full_messages显示错误信息。 - 还可以使用
flash消息,注意flash消息的声明周期是1个request,render不属于新request,要只在render里显示消息,使用flash.now。 - 登录使用
session,把session视为资源,在路由中resources :sessions, only;[:new, :create, :destory]限定,其中:new为登录页,:create为登录POST的endpoint,:destory为登出的endpoint。 - 登录表单使用
form_for(:session, url: sessions_path) do |f| ... end。 xxxHelp中的方法默认只能在试图中用,要在Controller中使用的话,必须在Controller中使用include xxxHelper引入。
- 完 -
若本文对您有帮助,欢迎微信打赏,感谢您的鼓励。
