Creating a new Rails project
Create two models (Parent & Child)
Review un-nested routes
Adding model relationships
Nesting the routes
Reviewing changes to routes
Adding test data via Rails console
Adding a private controller method to load the Parent object for each method
At this point, each controller and view for the Child class model needs to be adjusted (links, redirection, form, etc)
Method: children#index
Method: children#new
Method: children#create
Method: children#show
Method: children#edit
Method: children#update
Method: children#destroy
At this point, the default scaffolding's links and redirection have been updated to work with the nested routes.
$ mkdir family
# create rvm gemset
$ echo "rvm use --create ruby-1.9.2@family"> family/.rvmrc
$ cd family
# install rails
$ gem install rails
# create new rails project
$ rails new.# version control
$ git init
$ git add .
$ git commit -am "new rails project"
Create two models (Parent & Child)
# Parent model
$ rails generate scaffold Parent name:string
$ git add .
$ git commit -am "rails generate scaffold Parent name:string"# Child model
$ rails generate scaffold Child name:string parent_id:integer
$ git add .
$ git commit -am "rails generate scaffold Child name:string parent_id:integer"# Create db (defaults to SQLite3)
$ rake db:migrate
# version control
$ git add db/schema.rb
$ git commit db/schema.rb -m "created database schema"
Review un-nested routes
$ rake routes
children GET /children(.:format) children#index
POST /children(.:format) children#create
new_child GET /children/new(.:format) children#new
edit_child GET /children/:id/edit(.:format) children#edit
child GET /children/:id(.:format) children#show
PUT /children/:id(.:format) children#update
DELETE /children/:id(.:format) children#destroy
parents GET /parents(.:format) parents#index
POST /parents(.:format) parents#create
new_parent GET /parents/new(.:format) parents#new
edit_parent GET /parents/:id/edit(.:format) parents#edit
parent GET /parents/:id(.:format) parents#show
PUT /parents/:id(.:format) parents#update
DELETE /parents/:id(.:format) parents#destroy
Adding model relationships
# file: app/models/parent.rbclassParent<ActiveRecord::Base
attr_accessible :name
has_many :children
end# file: app/models/child.rbclassChild<ActiveRecord::Base
attr_accessible :name,:parent_id
belongs_to :parent
end# version control
$ git commit app/models -m "added relationships to models"
Nesting the routes
# file: config/routes.rb- resources :children
- resources :parents
+ resources :parents do+ resources :children
+end# version control
$ git commit -m config/routes.rb "nested resources in routes file"
Reviewing changes to routes
$ rake routes
parent_children GET /parents/:parent_id/children(.:format) children#index
POST /parents/:parent_id/children(.:format) children#create
new_parent_child GET /parents/:parent_id/children/new(.:format) children#new
edit_parent_child GET /parents/:parent_id/children/:id/edit(.:format) children#edit
parent_child GET /parents/:parent_id/children/:id(.:format) children#show
PUT /parents/:parent_id/children/:id(.:format) children#update
DELETE /parents/:parent_id/children/:id(.:format) children#destroy
parents GET /parents(.:format) parents#index
POST /parents(.:format) parents#create
new_parent GET /parents/new(.:format) parents#new
edit_parent GET /parents/:id/edit(.:format) parents#edit
parent GET /parents/:id(.:format) parents#show
PUT /parents/:id(.:format) parents#update
DELETE /parents/:id(.:format) parents#destroy
Adding test data via Rails console
$ rails c
> dad =Parent.new(:name =>'Paul')=>#<Parent id: nil, name: "Paul", created_at: nil, updated_at: nil> > dad.save
(0.1ms)begin transaction
SQL (20.0ms) INSERT INTO "parents"("created_at","name","updated_at") VALUES (?,?,?)[["created_at",Fri,06Apr201216:13:17 UTC +00:00],["name","Paul"],["updated_at",Fri,06Apr201216:13:17 UTC +00:00]](2.4ms) commit transaction
=>true> son = dad.children.new(:name =>'Eric')=>#<Child id: nil, name: "Eric", parent_id: 1, created_at: nil, updated_at: nil> > daughter = dad.children.new(:name =>'Mara')=>#<Child id: nil, name: "Mara", parent_id: 1, created_at: nil, updated_at: nil> >exit
Adding a private controller method to load the Parent object for each method
# file: app/controllers/children_controller.rb@@-1,4+1,7@@classChildrenController<ApplicationController++ before_filter :load_parent
+# GET /children# GET /children.jsondef index
@@-80,4+83,11@@classChildrenController<ApplicationController
format.json { head :no_content }endend++private++def load_parent
+@parent=Parent.find(params[:parent_id])+end+end
At this point, each controller and view for the Child class model needs to be adjusted (links, redirection, form, etc)
Method: children#index
# file: app/controllers/children_controller.rbdef index
-@children=Child.all
+@children=@parent.children.all
# file: app/views/children/index.html.erb-<td><%= link_to 'Show', child %></td>-<td><%= link_to 'Edit', edit_child_path(child)%></td>-<td><%= link_to 'Destroy', child, confirm:'Are you sure?', method::delete%></td>+<td><%= link_to 'Show', parent_child_path(@parent, child)%></td>+<td><%= link_to 'Edit', edit_parent_child_path(@parent, child)%></td>+<td><%= link_to 'Destroy',[@parent, child], confirm:'Are you sure?', method::delete%></td>-<%= link_to 'New Child', new_child_path %>+<%= link_to 'New Child', new_parent_child_path(@parent)%>
Method: children#new
# file: app/controllers/children_controller.rbdefnew-@child=Child.new+@child=@parent.children.new
# file: app/views/children/_form.html.erb-<%= form_for(@child)do|f|%>+<%= form_for([@parent,@child])do|f|%>
# file: app/views/children/new.html.erb-<%= link_to 'Back', children_path %>+<%= link_to 'Back', parent_children_path(@parent)%>
Method: children#create
# file: app/controllers/children_controller.rbdef create
-@child=Child.new(params[:child])+@child=@parent.children.new(params[:child])
respond_to do|format|if@child.save
- format.html { redirect_to @child, notice:'Child was successfully created.'}+ format.html { redirect_to [@parent,@child], notice:'Child was successfully created.'}
Method: children#show
# file: app/controllers/children_controller.rbdef show
-@child=Child.find(params[:id])+@child=@parent.children.find(params[:id])
# file: app/views/children/show.html.erb-<%= link_to 'Edit', edit_child_path(@child)%>|-<%= link_to 'Back', children_path %>+<%= link_to 'Edit', edit_parent_child_path(@parent,@child)%>|+<%= link_to 'Back', parent_children_path(@parent)%>
Method: children#edit
# file: app/controllers/children_controller.rbdef edit
-@child=Child.find(params[:id])+@child=@parent.children.find(params[:id])
# file: app/views/children/edit.html.erb-<%= link_to 'Show',@child%>|-<%= link_to 'Back', children_path %>+<%= link_to 'Show', parent_child_path(@parent,@child)%>|+<%= link_to 'Back', parent_children_path(@parent)%>
Method: children#update
# file: app/controllers/children_controller.rbdef update
-@child=Child.find(params[:id])+@child=@parent.children.find(params[:id])
respond_to do|format|if@child.update_attributes(params[:child])- format.html { redirect_to @child, notice:'Child was successfully updated.'}+ format.html { redirect_to [@parent,@child], notice:'Child was successfully updated.'}
Method: children#destroy
# file: app/controllers/children_controller.rbdef destroy
-@child=Child.find(params[:id])+@child=@parent.children.find(params[:id])@child.destroy
respond_to do|format|- format.html { redirect_to children_url }+ format.html { redirect_to parent_children_path(@parent)}
At this point, the default scaffolding's links and redirection have been updated to work with the nested routes.
相关推荐
母子亲情的散文精选.doc 资源标题:母子亲情的散文精选.doc 资源描述:母子亲情的散文精选.doc 资源标签:资料 资源内容: 母子亲情是人生中最为珍贵的感情。亲情是父母对儿女的爱,儿女对父母的感激和孝敬。...
针对煤化工厂某物料固含量高且颗粒粒度细、硬度高易造成管道结垢、堵塞等问题,提出一种母子旋流器进行在线除固的分离技术。试验研究了给料压力、底流口直径、溢流口直径等参数对母子旋流器分离性能的影响规律。正交...
《母子保健手册》工作制度是确保母婴健康和安全的一项重要机制,主要针对早孕期妇女和新生儿群体,包括流动人口和常驻人口。这一制度旨在通过系统化管理和追踪,为孕产妇和儿童提供全面的保健服务。 建册对象是制度...
在探讨青少年母子依恋与其网络成瘾之间关系的研究中,我们可以提炼出若干重要的知识点。首先,青少年作为互联网用户的重要组成部分,其互联网使用行为不仅受到个体心理发展的影响,还受到家庭环境尤其是与母亲之间...
【母子健康手册APP移动平台的构建与应用研究】 随着移动互联网技术的快速发展,移动应用在各个领域得到了广泛应用,其中包括医疗保健领域。针对妇女儿童这一特殊群体,母子健康手册APP的构建与应用成为了实现全程...
在JavaScript编程中,母子窗体间的通信是一个常见的需求,特别是在构建复杂的Web应用程序时。这个例子将探讨如何在两个浏览器窗口之间传递数据,主要聚焦于从父窗口向子窗口发送数据,以及子窗口如何接收并处理这些...
企业集团是以产权关系为基础的企业法人联盟,母子公司财务控制是集团核心管理内容的关键环节。有效的母子公司财务控制框架对于集团的持续发展和价值最大化至关重要。这一框架通常涵盖策略控制、组织控制、人员控制、...
妊娠期间母子免疫耐受的机制研究是理解母婴之间免疫调节的关键部分,对于胎儿的正常发育以及母亲的健康至关重要。研究表明,妊娠期间母亲和胎儿之间的双向血流交换,能够形成一种特殊的微嵌合状态,其中母体细胞可以...
APP版《母子健康手册》在孕产妇健康管理中的应用.pdf
煤炭集团母子公司管控体制 本文档讨论了煤炭集团母子公司管控体制的重要性和实施方法。煤炭集团母子公司管理是一种基本的保障体系,旨在确保企业集团或跨国公司内部权利分配、资产和战略管理、内部市场交易的有序...
接着,管理控制系统包括战略管理、审计、人事、财务、信息、审批权限和经营计划及预算控制。这些环节确保了母公司的管控力度和子公司运营的有序性。 最后,激励约束机制设计考虑了子公司经营者的激励力度、长期激励...
母子公司管理控制模式是企业集团运营中的关键环节,旨在通过有效的管控机制确保集团整体利益的最大化和可持续发展。这种管理模式涉及到多个层面,包括公司治理结构、管理制度、评估体系以及激励机制。 首先,母子...
【母子公司管控竞争策略】 母子公司管控竞争策略是企业集团管理的重要组成部分,旨在确保集团内各公司的协同效应、资源优化配置以及风险控制。在母子公司管控中,战略执行与财务管控是两个核心内容。 1. **战略...
母子房屋赠与合同.doc
《安徽某母子公司管理制度》是安徽省能源集团有限公司为了规范母子公司间的管理关系,提升集团整体运营效率和管理水平而制定的一套详细的操作指南。该文档由北大纵横管理咨询公司参与修订,旨在确保母子公司间的协同...
母子公司管控案例.pptx
母子亲情的散文.doc
母子型相似三角形是指在一系列相似三角形中,一个较大的三角形可以看作是另一个较小三角形的“母”三角形,而较小的三角形则是“子”三角形。这种关系对于解决涉及相似性质的问题非常有用。 1. **直角三角形相似** ...