Ruby on rails 对象关系和将对象ID传递给多个控制器?
我对做某事的最佳方式感到为难,希望得到一些建议 我有以下型号:Ruby on rails 对象关系和将对象ID传递给多个控制器?,ruby-on-rails,activerecord,table-relationships,Ruby On Rails,Activerecord,Table Relationships,我对做某事的最佳方式感到为难,希望得到一些建议 我有以下型号: class Plan < ActiveRecord::Base attr_accessible :description, :name, :user_id validates :name, :presence => true end class Planplace < ActiveRecord::Base attr_accessible :plan_id, :planplace validat
class Plan < ActiveRecord::Base
attr_accessible :description, :name, :user_id
validates :name, :presence => true
end
class Planplace < ActiveRecord::Base
attr_accessible :plan_id, :planplace
validates :planplace, :presence => true
end
class Plandate < ActiveRecord::Base
attr_accessible :plan_id, :plandate
validates :plandate, :presence => true
end
课程计划true
结束
类Planplacetrue
结束
类Plandatetrue
结束
一个计划可以有许多计划日期和计划地点。
我有一个控球器为每一个这样做一个非常基本的创建
class PlansController < ApplicationController
def create
@plan = Plan.new(params[:plan])
if @plan.save
flash[:success] = "Created ..."
else
flash[:alert] = "placeplace save error ..."
end
end
end
class PlanplacesController < ApplicationController
def create
@planplace = Planplace.new(params[:planplace])
if @planplace.save
flash[:success] = "Created ..."
else
flash[:alert] = "placeplace save error ..."
end
end
end
class PlandatesController < ApplicationController
def create
@plandate = Plandate.new(params[:plandate])
if @plandate.save
flash[:success] = "Created ..."
else
flash[:alert] = "plandate save error ..."
end
end
end
类平面控制器
我需要做的是使用一个视图创建一个平面,并允许使用该plan.plan\u id保存多个PlanDate和多个planplaces
在基本层面上实现这一点的最佳方法是什么(我的目标是在不重新加载的情况下进行ajax调用以进行保存,但我离这还很远,我仍在努力学习)
是否将其放置在单个“新”平面视图中?。我使用了一个表单,该表单使用fields_for更新同一表单中的多个模型,但在继续之前,我想在这里了解一下最好的方法
我知道我需要在模型中使用has_many和belish_,但我不确定进行完整保存的最佳方法,因此关系是正确的
希望我已经很好地解释了这一点。
任何关于最佳方法的建议都将不胜感激
(请客气一点,我才学了一个月的Rails)
---编辑---
我已经计算出了这里的模型,以及如何传递我需要的对象。我想我问的问题不对,因为我有点困惑。感谢所有看过它的人。退房
在您的情况下,一个计划
有许多:计划日期和有许多:计划地点。Plandate和Planplace都属于:plan
。然后,Plandates和Planplaces需要一个列,用于plan\u id:integer
我已将plan\u id添加到这两个表中。这对我来说都是新鲜事,所以尝试找到正确的解决方案不是“拼凑起来,但不是最好的”解决方案