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

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

问题描述

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

namespace :admin do资源:用户结尾

这意味着:

admin_user_index GET/admin/user(.:format) admin/user#indexPOST/admin/user(.:format) admin/user#createnew_admin_user GET/admin/user/new(.:format) admin/user#newedit_admin_user GET/admin/user/:id/edit(.:format) admin/user#editadmin_user GET/admin/user/:id(.:format) admin/user#showPUT/admin/user/:id(.:format) admin/user#update删除/admin/user/:id(.:format) admin/user#destroy

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

这是:

<块引用>

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

解决方案

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

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

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

which means this:

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

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>

which is this:

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天全站免登陆