gpt4 book ai didi

ruby-on-rails - 设计错误消息!为 nil :NilClass 导致未定义的方法 `errors'

转载 作者:数据小太阳 更新时间:2023-10-29 08:05:18 24 4
gpt4 key购买 nike

我的 layouts/application.html.erb 上有一个表单,发布在下面,如果我添加行 <%= devise_error_messages! %>我收到以下错误。

如何在没有分页符的情况下添加 devise_error_messages?

/app/views/layouts/application.html.erb

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %> <!-- this line causes issues -->
<h3>Add new contact</h3>
First Name<br />
<%= f.text_field :username %><br />
Last Name<br />
<%= f.text_field :password %><br />
<%= f.text_field :password_confirmation %><br />
Email<br />
<%= f.text_field :email %>
<hr />
<%= f.submit "Add Contact" %>

<% end %>

错误:

NoMethodError in My_devise/sessions#index

Showing /app/views/layouts/application.html.erb where line #59 raised:

undefined method `errors' for nil:NilClass

Extracted source (around line #59):

56: <a href="#" class="has-popupballoon button button-blue"><span class="add"></span>New Contact</a>
57: <div class="popupballoon top">
58: <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
59: <%= devise_error_messages! %>
60: <h3>Add new contact</h3>
61: First Name<br />
62: <%= f.text_field :username %><br />

这是我正在使用的网址 http://localhost:3000/admin/home这是我的 routes.rb

devise_for :users do 
get '/users/sign_out' => 'devise/sessions#destroy'
get "/users/sign_in", :to => "my_devise/sessions#new"
get 'admin/home', :to => 'my_devise/sessions#index'
get 'users/sign_up', :to => 'my_devise/registrations#new'
end

devise_for :users, :controllers => {:sessions => "my_devise/sessions", :registrations => "my_devise/registrations"}
get "home/index"
root :to => "home#index"

/app/controllers/my_devise/sessions_controller.rb

class MyDevise::SessionsController < Devise::SessionsController
layout false

before_filter :authenticate_user!

def index
render :layout => 'application'
end

def new
super
end

def create
super
end

end

/app/controllers/my_devise/registrations_controller.rb

class MyDevise::RegistrationsController < Devise::RegistrationsController

def index

end

end

rake 路线:

users_sign_out GET    /users/sign_out(.:format)      {:controller=>"devise/sessions", :action=>"destroy"}
users_sign_in GET /users/sign_in(.:format) {:controller=>"my_devise/sessions", :action=>"new"}
admin_home GET /admin/home(.:format) {:controller=>"my_devise/sessions", :action=>"index"}
users_sign_up GET /users/sign_up(.:format) {:controller=>"my_devise/registrations", :action=>"new"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"my_devise/sessions"}
POST /users/sign_in(.:format) {:action=>"create", :controller=>"my_devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"my_devise/sessions"}
POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
GET /users/cancel(.:format) {:action=>"cancel", :controller=>"my_devise/registrations"}
POST /users(.:format) {:action=>"create", :controller=>"my_devise/registrations"}
GET /users/sign_up(.:format) {:action=>"new", :controller=>"my_devise/registrations"}
GET /users/edit(.:format) {:action=>"edit", :controller=>"my_devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"my_devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"my_devise/registrations"}
home_index GET /home/index(.:format) {:controller=>"home", :action=>"index"}
root / {:controller=>"home", :action=>"index"}
root / {:controller=>"home", :action=>"index"}

最佳答案

我遇到了类似的问题。

我花了一段时间才弄明白,但就我而言,这是由于:

def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user

在我的 application_controller.rb 文件中。

我从教程中添加了这一行。删除它后,我不再收到有关“devise_error_messages”的错误。我不太明白为什么这会打乱事情,也许“current_user”是在别处定义的,它设置(或导致设置)错误对象。

关于ruby-on-rails - 设计错误消息!为 nil :NilClass 导致未定义的方法 `errors',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11219670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com