Rails:form_for命名空间资源 [英] Rails: form_for namespaced resource

查看:80
本文介绍了Rails:form_for命名空间资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为用户设置CRUD,仅对我的Web应用程序的管理员可用. 所以在routes.rb中:

I want to set up the CRUD for users, available just for administrators of my web application. So in routes.rb:

namespace :admin do
  resources :user
end

这意味着:

admin_user_index GET    /admin/user(.:format)                  admin/user#index
                 POST   /admin/user(.:format)                  admin/user#create
  new_admin_user GET    /admin/user/new(.:format)              admin/user#new
 edit_admin_user GET    /admin/user/:id/edit(.:format)         admin/user#edit
      admin_user GET    /admin/user/:id(.:format)              admin/user#show
                 PUT    /admin/user/:id(.:format)              admin/user#update
                 DELETE /admin/user/:id(.:format)              admin/user#destroy

显示,索引工作正常,但编辑和新建则不行.我在_form第一行中不断收到此错误:
未定义的方法"user_path",用于#<#:0x007fb6645c6378>

Show, index work fine but edit and new don't. I keep getting this error in the _form first line:
undefined method `user_path' for #<#:0x007fb6645c6378>

这是

如何将form_for与命名空间资源一起使用?

How can I use form_for with a namespaced resource?

推荐答案

您可以将名称空间的名称添加为符号:

You can add the namespace's name as a symbol:

 <%= form_for [:admin, @user] do |f| %>

这篇关于Rails:form_for命名空间资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆