我提交嵌套表单时遇到上述错误,我不确定如何修复它。这是一些信息。确切的错误是No route matches [POST] "/vehicles/2/vehicle_records/new"
。奇怪的是,当我运行rake路线时,路由存在,我看到以下条目
/vehicles/:vehicle_id/vehicle_records/new(.:format)
vehicle.rb
class Vehicle < ActiveRecord::Base
has_many :vehicle_records, dependent: :destroy
accepts_nested_attributes_for :vehicle_records
end
vehicle_record.rb
class VehicleRecord < ActiveRecord::Base
belongs_to :vehicle
end
我希望能够添加车辆展示视图中链接的车辆维护记录。
视图/车辆/ show.html.erb
<h3><b>Vehicle ID:</b> <%= @vehicle.id %></h3>
<p><b>Year:</b> <%= @vehicle.year %></p>
<p><b>Make:</b> <%= @vehicle.make %></p>
<p><b>Model:</b> <%= @vehicle.model %></p>
<p><b>Work Last Performed:</b> <%= @vehicle.make %></p>
<h2>Maintenance Record</h2>
<table>
<tr>
<th>Date Performed</th>
<th>Mileage</th>
<th>Hours</th>
<th>Work Performed</th>
</tr>
<% @vehicle.vehicle_records.each do |vr|%>
<tr>
<td><%= vr.date_performed %></td>
<td><%= vr.mileage %></td>
<td><%= vr.hours %></td>
<td><%= vr.work_performed %></td>
</tr>
<% end %>
</table>
<%= link_to "Add Maintenance Record", new_vehicle_vehicle_record_path(@vehicle) %>
然后我链接到vehicle_record新表格
视图/ vehicle_records / new.html.erb
<%= form_for :vehicle_record do |vr| %>
<p>
<%= vr.label :date_performed%><br />
<%= vr.text_field :date_performed%>
</p>
<p>
<%= vr.label :mileage%><br />
<%= vr.text_field :mileage%>
</p>
<p>
<%= vr.label :mileage%><br />
<%= vr.text_field :mileage%>
</p>
<p>
<%= vr.label :hours%><br />
<%= vr.text_field :hours%>
</p>
<p>
<%= vr.label :work_performed%><br />
<%= vr.text_area :work_performed%>
</p>
<p><%= vr.submit "Create Record" %></p>
我的车辆记录控制器如下
vehicle_records_controller.rb
class VehicleRecordsController < ApplicationController
def new
@vehicle_record = VehicleRecord.new
end
def create
@vehicle_record = VehicleRecord.new(params[:vehicle_record])
if @vehicle_record.save
redirect_to vehicles_path
else
flash[:notice] = "Not Saved"
end
end
end
的routes.rb
resources :vehicles do
resources :vehicle_records
end
答案 0 :(得分:1)
更新:没有嵌套表单
因此,我听说您只想在不触及vehicle_report
的情况下修改vehicle
。由于您已将vehicle_report
作为嵌套资源,因此可以按如下方式修改控制器:
class VehicleRecordController < ApplicationController
...
def new
@vehicle = Vehicle.find params[:vehicle_id]
@vehicle_record = @vehicle.vehicle_records.build
end
...
end
并且create
方法的内容相当容易。然后,您必须将表单更改为:
<%= form_for @vehicle_record do |v| %>
...
<%= # vehicle record attributes %>
...
<%= v.submit "Submit" %>
你很高兴去!
原始答案:w /嵌套表单
首先,我没有看到您使用任何嵌套表单。其次,我不知道你的vehicle_record
表单如何知道它实际属于哪个vehicle
(请参阅控制器)。在我看来,最简单的方法是允许使用nested forms直接在vehicle_records
或vehicles/:id/edit
中添加和删除vehicles/new
:
<%= form_for @vehicle do |v| %>
...
<%= # vehicle attributes here %>
...
<%= v.fields_for :vehicle_records do |vr| %>
...
<%= # vehicle record attributes %>
...
<%= v.submit "Submit" %>
对于现有的vehicles
,fields_for
将呈现所有现有vehicle_records
的列表,对于新的列表,您可以在控制器中使用以下代码进行fields_for
渲染,例如三空vehicle_records
:
class VehicleController < ApplicationController
...
def new
@vehicle = new Vehicle
3.times { @vehicle.vehicle_records.build }
end
...
end
如果您希望每次编辑现有vehicle_records
时都有一定数量的免费vehicle
,则可以使用类似的方法进行编辑并重复使用上述表单:
class VehicleController < ApplicationController
...
def edit
@vehicle = Vehicle.find params[:id]
# Add one blank vehicle record to existing ones
# This is also rendered by fields_for
@vehicle.vehicle_records.build
end
...
end