我创建了一个简单的 rails 项目。一切都很好,直到我尝试添加一个新模型Paintings
进行belongs_to
治疗和通过治疗Patient
进行has_many
绘画。所以不知何故我创建的嵌套表单没有出现,我相信它与控制器有关!谢谢,来自德国的问候!
治疗控制器:
class TreatmentsController < ApplicationController
def create
@patient = Patient.find(params[:patient_id])
@treatment = @patient.treatments.create(params[:treatment])
redirect_to patient_path(@patient)
end
def destroy
@patient = Patient.find(params[:patient_id])
@treatment = @patient.treatments.find(params[:id])
@treatment.destroy
redirect_to patient_path(@patient)
end
end
nested fields_for
并且没有显示治疗的形式:
<%= form_for([@patient, @patient.treatments.build]) do |f| %>
<div class="field">
<%= f.label :content %>
<%= f.text_area :content, :cols => "30", :rows => "10" %>
</div>
<div class="field">
<%= f.label :category_id %>
<%= f.collection_select :category_id, Category.find(:all), :id, :typ %>
</div>
<%= f.fields_for :paintings do |ff| %>
<div class="field">
<%= ff.label :name, 'Tag:' %>
<%= ff.text_field :name %>
</div>
<% end %>
<div class="field">
<%= f.submit nil, :class => 'btn btn-small btn-primary' %>
</div>
<% end %>
更新:
展会现场:
<% @patient.treatments.each do |treatment| %>
<tr>
<td><%= treatment.category.try(:typ) %></td>
<td><%= treatment.content %></td>
<td><%= treatment.day %></td>
<td><div class="arrow"></div></td>
</tr>
<tr>