我想用茧宝石的简单形式来嵌套父类窗体中的子窗体。Rails - Cocoon Gem - 填充外键id
我有建议和音调模型。
的关联是:
建议
has_one :pitch, inverse_of: :proposal
accepts_nested_attributes_for :pitch, reject_if: :all_blank, allow_destroy: true
间距
belongs_to :proposal, inverse_of: :pitch
在我的申请表,我有:
<%= f.simple_fields_for :pitch do |f| %>
<%= f.error_notification %>
<%= render 'pitches/pitch_fields', f: f %>
<% end %>
<%= link_to_add_association 'Pitch', f, :pitch, partial: 'pitches/pitch_fields' %>
在我的提议控制器,我有:
def new
@proposal = Proposal.new
@proposal.build_pitch
# authorize @proposal
end
def edit
@proposal.build_pitch unless @proposal.pitch
end
def proposal_params
params.require(:proposal).permit(:title, :description,
pitch_attributes: [:id, :problem, :solution, :remark, :_destroy ])
end
音高/ _pitch_fields.html.erb有:
<div class="nested-fields">
<div class="form-inputs">
<%= f.input :problem, as: :text,
# :label => text_for_problem(current_user, @pitch.proposal),
:label => 'What issue does this proposal address?',
:input_html => {:rows => 10} %>
</div>
当我保存这一切和尝试,形式加载,我可以填写表格,但PROPOSAL_ID外键保存为零。
如果我尝试编辑的形式,我得到一个错误,指出:
Couldn't find Pitch with ID=5 for Proposal with ID=10
当我尝试:
pitch.errors.full_messages
NameError: undefined local variable or method `pitch' for main:Object
我需要什么,以迫使PROPOSAL_ID属性做在音高表中使用提案ID设置?
编辑后保存表单时出现错误? – nathanvda
是 - 没错 – Mel