让model-database-controller-view组合起来运作、互相联动,从而可以创建一个基本的app。这个过程中,最关键是下面5大步骤。其中,step5是相对更多、更复杂。如下以创建group为例,进行说明。
Step 1: 建model
rails g model group title:string description:text
Step 2: 建数据库
rake db:migrate
Step 3: 建controller
rails g controller groups
Step 4: 建routes
resources :groups
Step 5: 在controller里,建各个action(又称method);以及建表单;如下。
一、首页,展示所有group
1. 建index action

2. 建index action 的表单
touch app/views/groups/index.html.erb
二、新建一个group
1. 建new action

2. 建new action 的表单
touch app/views/groups/new.html.erb
3. 建create action,它从new action接收表单数据后,输送给数据库

三、展示一个group
1. 建show action

2. 建show action 的表单
touch app/views/groups/show.html.erb
四、编辑一个group
1. 建edit action

2. 建edit action 的表单
touch app/views/groups/edit.html.erb
3. 建update action,它从edit action接收表单数据后,输送给数据库

五、删除一个group
1. 建destroy action

六、自定义method
controller页面中,在private下面的method,为自定义method

网友评论