2012-11-03 47 views
0

我在做一些事情的最佳途径,希望得到一些建议的泡菜。对象关系并将对象ID传递给多个控制器?

我有以下型号:

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 

的规划可以有很多plandates和planplaces。 我有一个控制器为每个这些做一个非常基本的创建。

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 

我需要做的是创建一个计划,允许多个plandates和几个planplaces使用该plan.plan_id保存一个单一的视图。

什么是基本层面实现这一目标的最佳方法(我将着眼于做Ajax调用保存无需重新加载,但我不远了这一点,我仍然努力学习)。

不要我把这个单一的“新”的平面图内?我玩过一个表单,它使用fields_for在同一个表单中更多地更新一个模型,但我希望得到一个最佳方法的总体思路,然后再着手处理。

我知道我需要利用模型中的的has_many和belongs_to的,但我不能确定做完整的保存如此的关系是正确的最好方法。

希望我解释得很好。 任何有关最佳方法的建议都非常感谢。

(现在一个月请善待,我只学习Rails)的

---编辑--- 我在这里工作了模型,以及如何通过我所需要的对象。我不认为我正确地问这个问题,因为我有点困惑。感谢所有看过它的人。

回答

1

退房nested model forms

在你的情况,计划has_many :plandateshas_many :planplaces。 Plandate和Planplace belongs_to :plan。 Plandates和Planplaces则需要plan_id:integer的专栏。

+0

欢呼的链接。 – vlwills

+0

我已经将plan_id添加到两个表中。这对我来说都是新事物,所以试图找到正确的解决方案,而不是“只集中在一起,但不是最好的”解决方案 – vlwills

相关问题