【问题标题】:How to create a new table Donor such that it belongs to another table Campaign and that their relationship is satisfied?如何创建一个新表 Donor 使其属于另一个表 Campaign 并且满足它们的关系?
【发布时间】:2011-12-17 10:16:50
【问题描述】:

我是 Rails 新手,刚刚尝试建立一个活动捐赠页面。我有一个单一的捐赠者对象,我试图将其与一个活动联系起来。我在 Donor 模型(donor.rb)中添加了以下内容:

class Donor < ActiveRecord::Base
    belongs_to :campaign
end

并且我在我的广告系列模型(campaign.rb)中添加了以下内容:

class User < ActiveRecord::Base
 has_many :donors,:dependent => :destroy
end

现在我该如何创建一个新的 Donor 对象以反映这种关系。 我想通过替换donors_controller.rb

def new
@donor = Donor.new

通过

def new
@campaign=current_campaign
@donor = @campaign.donors.build    

将完成这项工作。但我得到一个错误

undefined method `donors' for nil:NilClass

理想情况下,我想做的只是在其“显示”页面上显示特定活动的捐助者,我想在活动创建后立即将活动的 ID 链接为捐助者的 ID。我只是无法理解如何去做。 希望有人可以帮助我。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 belongs-to has-and-belongs-to-many


    【解决方案1】:

    看来@campaign 未在您的donors_controller 中设置。你是怎么设置的?

    【讨论】:

    • 好吧,我现在编辑了它,我添加了@campaign = current_campaign。即使现在它也不起作用:(
    • current_campaign 是什么样的?
    猜你喜欢
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多