如何创建一个新表 Donor,使其属于另一个表 Campaign 并且满足它们的关系?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的捐赠者控制器中似乎未设置
@campaign
。你想如何设置它?It would appear that
@campaign
is not set in your donors_controller. How are you trying to set it?