在我的rails应用程序中有一个部分包含在新操作和编辑操作之间共享的表单:
<%= form_for @customer do |f| %>
....
<% end %>
这些操作属于控制器(称为客户)命名空间(称为 admin ),如果尝试运行代码,则在执行form_for时显示错误:
未定义的方法`customer_path'
使用以下方法解决了这个问题:
&lt;%= form_for:客户,@客户做 | F | %GT;
.... <% end %>
现在,当通过新操作调用时,表单会生成正确的网址,但是当由编辑生成时,表单网址为“/ admin / customers / 1 / 编辑“而不是更新。如果提交表单显示错误:
没有路线匹配“/ admin / customers / 1 / edit”
但在routes.rb中有:
命名空间:admin do
资源:客户
端
和rake:routes显示所有REST URL:
admin_customers GET /admin/customers(.:format){:action =&gt;“index”,:controller =&gt;“admin / customers”} POST /admin/customers(.:format){:action =&gt;“create”,:controller =&gt;“admin / customers”} new_admin_customer GET /admin/customers/new(.:format) {:action =&gt;“new”,:controller =&gt;“admin / customers”} edit_admin_customer GET / admin / customers /:id / edit(.: format){:action =&gt;“edit”,:controller =&gt;“admin / customers”} admin_customer GET /admin/customers/:id(.:format){:action =&gt;“show”,:controller =&gt;“admin / customers”} PUT /admin/customers/:id(.:format) {:action =&gt;“update”,:controller =&gt;“admin / customers”} DELETE /admin/customers/:id(.:format){:action =&gt;“destroy”,:controller =&gt;“admin / customers”}
有什么想法吗?
答案 0 :(得分:3)
试试这个
<%= form_for [:admin, @customer] do |f| %>