我有一个form_for,如下所示:
<div class="modal-body">
<%= form_for @folder, url: add_client_folders_path, html: { id: "add_client" } do |f| %>
<%= f.fields_for :client_profiles do |client_builder| %>
<%= client_builder.select :id, @clients.collect {|client| [ client.name, client.id ] }, { include_blank: true }, {class: "form-control"} %>
<% end %>
<% end %>
</div>
<div class="modal-footer">
<%= submit_tag "Add Client", class: "btn btn-primary", form: "add_client" %>
</div>
提交表单时,params哈希看起来像这样:
参数:{&#34; utf8&#34; =&gt;&#34;✓&#34;,&#34; authenticity_token&#34; =&gt;&#34; 7qxjMI1H / + uMP4qZWwG4lHxdrBBM1z&#34;, &#34;夹&#34; =&GT; {&#34; client_attributes&#34; =&GT; {&#34; 0&#34; =&GT; {&#34; ID&#34; =&GT;&#34 ; 25&#34;}}},&#34;提交&#34; =&gt;&#34;添加客户&#34;,&#34; id&#34; =&gt;&#34; add_client&#34;}
它给出了以下错误:
Couldn't find Folder with 'id'=add_client
为什么Rails在哈希中提交html id属性会导致此错误?
即使我删除了ID,它仍然会给我这个错误。路线看起来像这样:
resources :folders do
post :add_client, on: :collection
end
当我明确声明post方法时,它提交正常:
method: :post
但为什么我必须在这里指定方法帖?
答案 0 :(得分:0)
对于未来的读者,我比创建单独的资源路径更好。您可以继续重用更新的restful资源,因为更新现有记录时将自动保存新关联:
<%= form_for @folder do |f| %>
<%= f.fields_for :client_profiles, f.object.client_profiles.build do |client_builder| %>
<%= client_builder.select :id, @clients.collect {|client| [ client.full_name, client.id ] }, { include_blank: true }, {class: "form-control"} %>
<%= f.submit "Add Client", class: "btn btn-primary" %
&GT;