如何创建一个新表 Donor,使其属于另一个表 Campaign 并且满足它们的关系?

发布于 2024-12-21 14:09:06 字数 730 浏览 1 评论 0原文

我是 Rails 新手,刚刚尝试建立一个竞选捐赠页面。我有一个捐赠者对象,我试图将其与活动相关联。我已将以下内容添加到捐赠者模型(donor.rb):

class Donor < ActiveRecord::Base
    belongs_to :campaign
end

并且我已在我的活动模型(campaign.rb)中添加以下内容:

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

现在我如何创建一个新的捐赠者对象以反映这种关系。 我认为通过替换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。我只是不明白该怎么做。 希望有人能帮助我。

I am new to rails and have just been trying to build a campaign donation page. I have a single Donor object I am trying to relate to a Campaign. I have added the following to the Donor model(donor.rb):

class Donor < ActiveRecord::Base
    belongs_to :campaign
end

and I have added the following in my Campaign model(campaign.rb) :

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

Now how do I go about creating a new Donor object such that it reflect this relationship.
I thought by replacing in donors_controller.rb

def new
@donor = Donor.new

by

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

will do the job. But I get an error

undefined method `donors' for nil:NilClass

Ideally what I would like to do is just display the donors of a particular campaign on its 'show' page and I wanted to link the id of campaign as the id of donor as soon as it is created.I just cant understand how to go about it.
Hope somebody can help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2024-12-28 14:09:06

您的捐赠者控制器中似乎未设置@campaign。你想如何设置它?

It would appear that @campaign is not set in your donors_controller. How are you trying to set it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文