未定义的方法“模型”;对于用户

发布于 2024-10-19 14:46:18 字数 770 浏览 1 评论 0原文

我在 Rails 3 应用程序中进行了身份验证 托尼的教程和这个第二个教程

但我得到

用户未定义方法“模型”

这只是从教程中复制/粘贴,我认为这是我的应用程序架构中的某个人的问题:)

当我尝试访问 /users/new

I made authentication in my Rails 3 app fallowed by Tony's tutorial and this second tutorial.

But I get

undefined method 'model' for User

It's just copy/paste from tutorial, and I think that is problem someone in my app architecture :)

I get error when I try access to /users/new

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

渡你暖光 2024-10-26 14:46:18

我无法重现您在本地遇到的问题,这是我最好的猜测:@user 在您的视图中存在的唯一原因是因为您在控制器中调用 load_and_authorize_resource,这是创建 @user 的 CanCan 方法目的。这可能是问题的根源,因此您可以尝试使用authorize_resource,然后在新方法中显式创建@user。

以下是我注意到的几件事:

  1. devise 的标准创建用户路径是 /users/sign_up 但您使用的是 /users/new ,它指向不属于 devise 的自定义用户控制器,这在以下情况下会很成问题:使用像可确认这样的设计模块,因为它不会创建确认令牌或发送电子邮件。
  2. 您已经创建了一个用于注册的自定义控制器,但尚未告诉 devise。您需要执行类似 devise_for :users, :controllers => 的操作{:注册=> "users/registrations" } 在你的routes.rb 文件中,以便设计使用你的控制器。
  3. 进入注册页面加载后,您需要将first_name和last_name添加到数据库中,否则您将不会收到任何方法错误。

在深入研究您当前的问题之前,我将首先解决这些问题。

在开始添加报告界面之前,可能值得将代码精简为 Tony 所拥有的代码并使其正常工作。我看到您的数据库的角色列表为“super_admin”、“global_user”和“internal_user”,但您在查询之前在字符串上调用了camelize,所以我怀疑 CanCan 在您的环境中是否正常工作。

最后一件事,请确保更改所有密码和您共享的所有内容 - 请查看 这篇文章提供了要更改的文件列表

I am unable to reproduce the problem that you are having on my local be here is my best guess: The only reason @user exists on in your view is because you are calling load_and_authorize_resource in your controller which is a CanCan method that creates the @user object. This might be the source of your problem so you could try using authorize_resource and then explicitly creating @user in your new method.

Here are a couple of things that I noticed:

  1. The standard create user path for devise is /users/sign_up but you are using /users/new which points to your custom users controller that is not part of devise, this will be very problematic when using devise modules like confirmable because it won't create the confirmation tokens or send out the emails.
  2. You have created a custom controller for registrations but you haven't told devise about it. You will need to do something like devise_for :users, :controllers => { :registrations => "users/registrations" } in your routes.rb file for devise to use your controller.
  3. Once you get to the registration page to load you'll need to add first_name and last_name to your database otherwise you'll get no method errors.

I would start by straightening out these problems before diving into your current problem.

It might be worth stripping the code down to just what Tony has and get that working before you start adding your reports interface. I saw that your database has the roles lists as "super_admin", "global_user" and "internal_user" but you are calling camelize on the string before your query so I doubt that CanCan is working correctly in your environment.

One last thing, please make sure you change all the passwords and everything that you have shared - take a look at this post for a list of files to change.

九八野马 2024-10-26 14:46:18

要自定义由 Devise 创建的视图,首先在您的应用程序中生成它们。

railsgeneratedevise:views

这会导入您需要的所有视图,您可以根据您的喜好自定义它们。 中的代码

这是新用户视图 (/devise/registrations/new.html) '<%= form_for(resource, :as => resources_name, :url => Registration_path(resource_name)) do |f| %>'

您会注意到没有@user - 正是使用@user 引发了未定义的方法模型错误。

不知道为什么使用资源而不是@user,但希望这能让你继续下去。

To customise the views created by Devise first generate the them in your application.

rails generate devise:views

This imports all the views you need and you can them customise to your hearts content. This is the code from the new user view (/devise/registrations/new.html)

'<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>'

you will note that there is no @user - it is the use of @user that is throwing the undefined method model error.

No idea why resource is used and not @user but hopefully this will get you going.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文