Rails 简单的小例子 —— 新建一个购物网站

创建Shop项目

 $ rails new shop 

利用scaffold创建资源

 $ rails generate scaffold production name:text price:float amount:integer 

这里写图片描述

显示rails为资源分配的routes地址

 $ rake routes | grep production 

这里写图片描述

DB Migrate

 $ bin/rails db:migrate RAILS_ENV=development 

这里写图片描述

Start Server

development环境下,修改code无需重启server。

 $ rails server 

这里写图片描述

设置首页为productions/index

在Routes.rb中进行设置,有2种设置方法

 get '/' => 'productions#index' 
 root 'productions#index' 

这里写图片描述

给资源添加访问地址

 get '/addProduction' => 'productions#new' 

这里写图片描述

match 规则 & 传参
这里写图片描述

这里写图片描述

如果需要传参

 match '/addProduction/:name/:price' => 'productions#new' 

那么之后就可以在controller中获取这两个参数

 @name = params[:name] 

这里写图片描述

这里写图片描述

这里写图片描述

rails 常用命令

Usage: rails COMMAND [ARGS]

The most common rails commands are:
generate    Generate new code (short-cut alias: "g")
console     Start the Rails console (short-cut alias: "c")
server      Start the Rails server (short-cut alias: "s")
test        Run tests (short-cut alias: "t")
dbconsole   Start a console for the database specified in config/database.yml
(short-cut alias: "db")
new         Create a new Rails application. "rails new my_app" creates a
new application called MyApp in "./my_app"

All commands can be run with -h (or --help) for more information.

generate 常用命令

esxi23v113@esxi23v113:~/Aptana Studio Workspace/blog$ rails generate -h
Running via Spring preloader in process 11877
Expected string default value for '--jbuilder'; got true (boolean)
Usage: rails generate GENERATOR [args] [options]

General options:
-h, [--help]     # Print generator's options and usage
-p, [--pretend]  # Run but do not make any changes
-f, [--force]    # Overwrite files that already exist
-s, [--skip]     # Skip files that already exist
-q, [--quiet]    # Suppress status output

Please choose a generator below.

Expected string default value for '--helper'; got true (boolean)
Expected string default value for '--assets'; got true (boolean)
Rails:
assets
channel
controller
generator
helper
integration_test
jbuilder
job
mailer
migration
model
resource
scaffold
scaffold_controller
task

Coffee:
coffee:assets

Js:
js:assets

TestUnit:
test_unit:generator
test_unit:plugin
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值