Rails 命名空间错误? [英] Rails namespacing error?

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

问题描述

我的 config/routed.db 中有这个:

namespace :admin do
  resources :users
  resources :events
end

我使用 Rails 提供的脚手架生成了 User 模型,然后我简单地使用 Rails 的 generate 生成了一个管理员/管理员控制器,并简单地移动了所有用户 -相关的东西到控制器/视图/助手内的管理子目录中.是的,我确实必须在几个地方添加 admin_ 并且在表单部分内部我不得不将 form_for(@user) 更改为 form_for([:admin,@user]).

I generated the User model by using the scaffolding Rails provides, then I simply generated a admin/admin controller by using Rails' generate and simply moved all the user-related stuff into the admin sub-directories inside the controllers/views/helpers. Yes, I did have to add admin_ in a few places and inside the form partial I had to change the form_for(@user) to form_for([:admin, @user]).

当我尝试创建一个新用户时(这是当我POST用户数据时)我收到以下错误消息:

When I try to create a new user (this is when I POST the user data) I get the following error message:

undefined method `user_url' for #<Admin::UsersController:0x13f408e0>

应用程序级跟踪显示此处引发错误:

The application-level trace shows that the error is raised here:

app/controllers/admin/users_controller.rb:47:in `create'

出现此错误的代码行:

format.html { redirect_to @user, :notice => 'User was successfully created.' }

上一行在 respond_to 块内.

所以,我有两个问题:

  1. 我该如何解决这个问题?
  2. 有没有更聪明、更好的方法来做到这一点?


此外,如果您建议我以更智能、更好的轨道方式重新执行此操作,则可以获得加分!好吧,无论如何我都会这样做!:D


Also, bonus points if you suggest I re-do this in the smarter, better, rails-way! Well, I will do that anyway! :D

哦,我差点忘了一些可能相关的东西!我还让 Admin::UsersControllerAdmin::AdminController 继承(通常又从 ApplicationController 继承):

Oh, I almost forgot something that might be relevant! I also made Admin::UsersController inherit from Admin::AdminController (which normally in turn inherits from ApplicationController):

Admin::UsersController

Admin::AdminController

推荐答案

您也需要在重定向中使用命名空间:

You need to use the namespace in your redirect, too:

redirect_to [:admin, @user] #...

redirect_to admin_user_path(@user) #...

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

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