devise_error_messages! causes undefined method `er

2019-05-10 19:05发布

问题:

I have a form on my layouts/application.html.erb that is posted below and and if i add the line <%= devise_error_messages! %> I get the error below.

How can i add the devise_error_messages without having the page break?

/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 %>

Error:

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 />

This is the url i'm using http://localhost:3000/admin/home and here's my 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 routes:

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"}

回答1:

I had what may be a similar problem.

It took me awhile to figure it out, but in my case it was due to this :

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

in my application_controller.rb file.

I added this line from a tutorial. After I removed this, I received no more errors about "devise_error_messages". I don't quite understand why this would upset things, perhaps 'current_user' is defined elsewhere and it sets up (or leads to the setting up) of the error object.



回答2:

Try changing your devise_for routing, checkout this documentation http://rdoc.info/github/plataformatec/devise#Configuring_routes

You can add your own prefix with the :path parameter

Here is a sample

devise_for :users, :path => "", :path_names => {
 :sign_in => 'signin',
 :sign_out => 'signout',
 :sign_up => 'signup'            
 },
:sign_out_via => [:delete],

 :controllers => {:sessions => 'my_devise/sessions', :registrations => 
  'my_devise/registrations'}


回答3:

Revisiting a project I haven't had time to touch in a while, but the simple answer was just to create the User object in the application_controller.

I added this snippet to the application_controller:

before_filter :instantiateUser

def instantiateUser
    @user = User.new
end