设计中没有方法错误::注册#new [英] NoMethodError in Devise::Registrations#new
问题描述
当我尝试注册我的应用程序时,我不断收到此错误,我正在使用 devise 进行身份验证;
I keep getting this error when I try to sign up on my application, I am using devise for authentication;
NoMethodError in Devise::Registrations#new
undefined method `registration_path' for #<#<Class:0x007fe45c6c35e8>:0x007fe45d9ffd78>
Extracted source (around line #3):
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
当我运行 rake routes
时,我得到了;
When I run rake routes
I get;
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
注册链接代码;
<%= link_to "Sign up", new_user_registration_path%>
更新感谢 ParaPenguin,注册字段现在可以使用了,但是当我点击提交时,我不断遇到这个问题
UPDATE Thanks to ParaPenguin the sign up field works now, but when I click submit I keep getting this problem
No route matches [POST] "/users/sign_up"
new_user_session_path GET /users/sign_in(.:format) devise/sessions#new
user_session_path POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session_path DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password_path POST /users/password(.:format) devise/passwords#create
new_user_password_path GET /users/password/new(.:format) devise/passwords#new
edit_user_password_path GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration_path GET /users/cancel(.:format) devise/registrations#cancel
user_registration_path POST /users(.:format) devise/registrations#create
new_user_registration_path GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration_path GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format)
更新 2-Gjaldon 要求我包含我的用户模型和路线我的用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Routes.rb
Document::Application.routes.draw do
devise_for :users
root "pages#home"
get "about" => "pages#about"
get "prices" => "pages#prices"
get "faq" => "pages#faq", :as => :faq
get "terms" => "pages#terms"
get "view" => "pages#view"
get "policy" => "pages#policy"
get "contact" => "pages#contact"
get "works" => "pages#works"
推荐答案
你能否在你的 routes.rb 中提供你的设计路线和你的用户模型的设计代码?
Could you provide your routes for Devise in your routes.rb and the devise code for your User model?
以下任一方法可能会解决您的问题:
It's either one of the following that might fix your problem:
重启你的 Rails 服务器.这样,Rails 就会看到 Devise 对 Rails 所做的新文件和更改.
Restart your Rails server. This way, Rails will see the new files and changes Devise has made to Rails.
您是否覆盖了 Devise::Registrations#new 操作?如果是这样,您需要替换下面的视图代码:
Did you override the Devise::Registrations#new action? If so, you will need to replace your view code below:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|%>
与:
<%= form_for(resource, :as => resource_name, :url => user_registration_path) do |f| %>
在您的 devise_registrations/edit.html.erb
视图中,您的 form_for 代码如下所示:
In your devise_registrations/edit.html.erb
view, your form_for code would look like:
<%= form_for(resource, :as => resource_name, :url => edit_user_registration_path(resource_name, :method => :put) do |f| %>
你的 rake 路由
实际上为你提供了你需要提交的路由.请记住,相应的控制器和动作显示在 rake routes
输出的 Controller#action 列下.edit
操作中的表单应始终提交给 update
操作,new
操作中的表单应始终提交给 create
action 如果你坚持 Rails 约定.如果您决定在另一个操作中使用表单来创建记录,请确保将表单提交给 create
操作.
Your rake routes
actually provides you with the routes you need to submit to. Just keep in mind that the corresponding controller and action is displayed under the Controller#action column in the output of rake routes
. The form in edit
action should always submit to the update
action and the form in new
action should always submit to the create
action if you stick to the Rails conventions. If you decide to have a form in another action for creating a record, then make sure to have the form submit to the create
action.
这篇关于设计中没有方法错误::注册#new的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!