Rails has_many 嵌套形式 没有路由匹配 [POST]
Rails has_many nested form No route matches [POST]
我在提交嵌套表单时遇到上述错误,但我不确定如何修复它。这是一些信息。确切的错误是 No route matches [POST] "/vehicles/2/vehicle_records/new"
。奇怪的是,当我 运行 rake routes 我看到以下条目时,路由存在
/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
我希望能够添加从车辆显示视图 link 编辑的车辆维修记录。
views/vehicle/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) %>
然后我link到vehicle_record新表格
views/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
更新:w/o 嵌套形式
所以我听说您只想编辑 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" %>
一切顺利!
原始答案:带嵌套形式
首先,我没有看到您使用任何嵌套表单。其次,我看不出您的 vehicle_record
表单如何知道它实际上属于哪个 vehicle
(请参阅您的控制器)。在我看来,最简单的方法是允许使用 nested forms:
直接在 vehicles/:id/edit
或 vehicles/new
中添加和删除 vehicle_records
<%= 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
时总是有一些免费的 vehicle_records
,您可以使用类似的方法进行编辑并重复使用上述表格:
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
我在提交嵌套表单时遇到上述错误,但我不确定如何修复它。这是一些信息。确切的错误是 No route matches [POST] "/vehicles/2/vehicle_records/new"
。奇怪的是,当我 运行 rake routes 我看到以下条目时,路由存在
/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
我希望能够添加从车辆显示视图 link 编辑的车辆维修记录。
views/vehicle/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) %>
然后我link到vehicle_record新表格
views/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
更新:w/o 嵌套形式
所以我听说您只想编辑 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" %>
一切顺利!
原始答案:带嵌套形式
首先,我没有看到您使用任何嵌套表单。其次,我看不出您的 vehicle_record
表单如何知道它实际上属于哪个 vehicle
(请参阅您的控制器)。在我看来,最简单的方法是允许使用 nested forms:
vehicles/:id/edit
或 vehicles/new
中添加和删除 vehicle_records
<%= 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
时总是有一些免费的 vehicle_records
,您可以使用类似的方法进行编辑并重复使用上述表格:
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