Rails 5:嵌套表单设置正确吗



如果我想要volunteer_shift的嵌套表单在我的作业中/\uform

应该如何建立我的模特协会?

我目前的联想是这样的。。但我不确定他们是否正确,如果志愿者的轮班部分位于分配表中…

当我试图打开";添加新记录";表单…我的作业#新操作出错。。。

错误消息

Completed 500 Internal Server Error in 30ms (ActiveRecord: 2.7ms)

NoMethodError - undefined method `build' for nil:NilClass:
app/controllers/assignments_controller.rb:21:in `new'

这是我的新动作。。。

# GET /assignments/new
def new
@assignment = Assignment.new
# binding.pry
@assignment.volunteer_event.build   <----Line 21
@my_url = {:action => "create", :id => params[:id]}
end

以下是我目前的模特协会。。。

class Assignment < ApplicationRecord
attr_accessor :volunteer_event
belongs_to :volunteer_shift
has_one :volunteer_task_type, :through => :volunteer_shift, :source => :volunteer_task_type
belongs_to :contact ,optional: true
validates_presence_of :volunteer_shift #belongs_to takes care of this now
validates_associated :volunteer_shift
accepts_nested_attributes_for :volunteer_shift, allow_destroy: true
...
class VolunteerShift < ApplicationRecord
has_many :assignments
belongs_to :volunteer_event
...
class VolunteerEvent < ApplicationRecord
belongs_to :volunteer_default_event
has_many :volunteer_shifts, :dependent => :destroy
has_many :resources_volunteer_events, :dependent => :destroy
validates_associated :volunteer_shifts

在我的任务/表格中

<%= form_for @assignment, :url => @my_url, remote: true do |f| %>
...
<!--VOLUNTEER SHIFT-->
<!--TODO: make this a partial under field_for-->
<%= f.fields_for :volunteer_shift do |builder| %>
<%= render 'volunteer_shift_fields', vs: builder %>
<% end %>
...
<% end %>

如果你想看到volunteer_shift_fields部分


<div class="name large flex-row">
<%= vs.label :volunteer_shift %>
</div>
<div id="volunteer_shift" class="d-flex flex-row">
<div class="col-sm-12 p-2">
<div id="volunteer_shift" class="text-right">
<!-- old if: if class is assignment show volunteer shift else show default shift -->
<!--  we need default shift here...NO assignment is attached-->
<div class="field">
<%= vs.label :volunteer_task_type_id %>
<%= select_tag 'volunteer_task_type_id', options_from_collection_for_select([VolunteerTaskType.new(:description => ""),  VolunteerTaskType.instantiables.effective_on(Date.today)].flatten, "id", "description")  %>
</div>
<div class="field">
<%= vs.label :roster_id %>
<%= select_tag 'roster_id', options_from_collection_for_select([Roster.new(:name => ""), Roster.all].flatten, "id", "name") %>
</div>
<div class="field">
<%= vs.label :program_id %>
<%= select_tag 'program_id', options_from_collection_for_select([Program.new(:name => ""), Program.where(:volunteer => true)].flatten, "id", "name")%>
</div>
<div class="field">
<%= vs.label :set_description %>
<%= vs.text_field(:set_description, nil) %>
</div>
<div class="field">
<%= vs.label :set_date, "Date" %> <
<%= vs.text_field(:set_date, nil) %>
</div>
</div>
</div>
</div>

我的模型关联正确吗?什么是我的";分配#new";缺少操作?

删除attr_accessor:volunteer_event,需要通过volunteer_shift 设置到volunteer_event的模型关联

所以

class Assignment < ApplicationRecord
attr_accessor :volunteer_event

需要成为

class Assignment < ApplicationRecord
has_one :volunteer_event, through: :volunteer_shift

看看你的志愿者轮班协会,我很确定你只想在一次任务中有一个志愿者事件记录?如果这不是你想要的,那么你需要再次查看你的关联

或者。。。

您可以将attr_accessor更改为attr_reader,并在初始化器中分配相关的volunteer_event记录,但我不建议在这种情况下使用

相关内容

  • 没有找到相关文章

最新更新