提交表单时出现以下错误:
No route matches [PATCH] "/foreclosure_defenses/4/properties"
即使浏览器中的html如下所示:
<form accept-charset="UTF-8" action="/foreclosure_defenses/4/properties" class="form-horizontal" id="edit_foreclosure_defense_4" method="post">
由于新表单呈现了多态模型和属性模型,因此我创建了我的表单:
<%= form_for @owner, url: polymorphic_path([@owner, @property]) do |f| %>
<%= f.fields_for :properties, @property do |property_builder| %>
<%= render 'form', f: property_builder %>
<% end %>
<% end %>
为什么我收到此PATCH错误?表单是用POST提交的,应该转到CREATE方法。
以下是相关路线:
foreclosure_defense_properties_path GET /foreclosure_defenses/:foreclosure_defense_id/properties(.:format) properties#index
POST /foreclosure_defenses/:foreclosure_defense_id/properties(.:format) properties#create
new_foreclosure_defense_property_path GET /foreclosure_defenses/:foreclosure_defense_id/properties/new(.:format) properties#new
edit_foreclosure_defense_property_path GET /foreclosure_defenses/:foreclosure_defense_id/properties/:id/edit(.:format) properties#edit
foreclosure_defense_property_path GET /foreclosure_defenses/:foreclosure_defense_id/properties/:id(.:format) properties#show
PATCH /foreclosure_defenses/:foreclosure_defense_id/properties/:id(.:format) properties#update
PUT /foreclosure_defenses/:foreclosure_defense_id/properties/:id(.:format) properties#update
DELETE /foreclosure_defenses/:foreclosure_defense_id/properties/:id(.:format) properties#destroy
答案 0 :(得分:0)
最终工作的解决方案明确定义了http方法:
<%= form_for @owner, url: polymorphic_path([@owner, @property]), method: :post, html: {class: "form-horizontal"} do |f| %>
不确定为什么要这样做。在我看来,Poymorphic路径应该足够聪明,以确定它是一个创建动作。