未定义的方法“模型”;对于用户
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法重现您在本地遇到的问题,这是我最好的猜测:@user 在您的视图中存在的唯一原因是因为您在控制器中调用 load_and_authorize_resource,这是创建 @user 的 CanCan 方法目的。这可能是问题的根源,因此您可以尝试使用authorize_resource,然后在新方法中显式创建@user。
以下是我注意到的几件事:
在深入研究您当前的问题之前,我将首先解决这些问题。
在开始添加报告界面之前,可能值得将代码精简为 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:
devise_for :users, :controllers => { :registrations => "users/registrations" }
in your routes.rb file for devise to use your controller.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.
要自定义由 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.