3

我的 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

耙路线:

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

3 回答 3

2

我遇到了类似的问题。

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

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”是在别处定义的,它设置(或导致设置)错误对象。

于 2012-08-05T12:22:40.200 回答
0

尝试更改您的 devise_for 路由,查看此文档http://rdoc.info/github/plataformatec/devise#Configuring_routes

您可以使用 :path 参数添加自己的前缀

这是一个示例

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'}
于 2012-06-27T05:14:54.597 回答
0

重新访问一个我有一段时间没有时间接触的项目,但简单的答案就是在 application_controller 中创建 User 对象。

我将此片段添加到 application_controller:

before_filter :instantiateUser

def instantiateUser
    @user = User.new
end
于 2012-10-15T01:45:11.660 回答