Rails +为nil设计未定义的方法`errors':NilClass

时间:2014-02-27 22:17:36

标签: ruby-on-rails devise

我在使用我的rails应用中的Devise加载新的注册页面时遇到问题。当我尝试加载注册页面时,出现错误:

undefined method `errors' for nil:NilClass

这似乎是由以下行引起的:

 <%= devise_error_messages! %>

我可能做错了什么?

routes.rb中:

  devise_for :users, :controllers => {:registrations => :registrations, omniauth_callbacks: "users/omniauth_callbacks"} 

  devise_scope :user do
      post 'registrations' => 'registrations#create', :as => 'register'
      post 'sessions' => 'sessions#create', :as => 'login'
      delete 'sessions' => 'sessions#destroy', :as => 'logout'
  end

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  # GET /user/sign_up
  def new
    build_resource({})
    respond_with self.resource
  end

注册链接:

<%= link_to "Sign Up", new_registration_path(resource_name), :class=>"btn btn-mini", :style=>'float:left'%>

application_helper.rb(其中定义了resource_name)

def resource_name
    :user
end

def resource
    @resource ||= User.new
end

def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
end

注册视图

<div class="signUpPage">

  <div class="form_container">

        <h3 class="form_heading sign_up">Sign up</h3>

        <%= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>

          <%= devise_error_messages! %>

          <div class="formField"><%= f.label :username, :class=> "label label-info" %><br />
          <%= f.text_field :username, :class=> "signup_username", :maxlength=>"15"%></div>
          <div class="usernameValidation validation">Username is already taken</div>

          <div class="formField"><%= f.label :email, :class=> "label label-info" %><br />
          <%= f.email_field :email%></div>
          <div class="emailValidation validation">Email is already taken</div>

          <div class="formField"><%= f.label :password, :class=>"label label-info" %><br />
          <%= f.password_field :password, :class=> "signup_password" %></div>
          <div class="passwordValidation validation">Password must be at least 8 characters</div>

          <div class="formField"><%= f.label :password_confirmation, :class=>"label label-info" %><br />
          <%= f.password_field :password_confirmation %></div>
          <div class="passwordConfirmationValidation validation">Passwords do not match</div>

          <div><%= f.submit "Sign up", :class=>"btn btn-info signup_button" %></div>
        <% end %>

        <%= render "devise/shared/links" %>

    </div>
</div>

这是完整的错误消息:

NoMethodError in Registrations#new

Showing Website/app/views/devise/registrations/new.html.erb where line #12 raised:

undefined method `errors' for nil:NilClass
Extracted source (around line #12):

9: 
10:             <%= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), namespace: 'sign_up', html: {id: 'form_user_sign_up'}) do |f| %>
11:         
12:               <%= devise_error_messages! %>
13: 
14:               <div class="formField"><%= f.label :username, :class=> "label label-info" %><br />
15:               <%= f.text_field :username, :class=> "signup_username", :maxlength=>"15"%></div>
Rails.root: /Website

app/views/devise/registrations/new.html.erb:12:in `block in _app_views_devise_registrations_new_html_erb___65153715043208623_70149577578880'
app/views/devise/registrations/new.html.erb:10:in `_app_views_devise_registrations_new_html_erb___65153715043208623_70149577578880'
app/controllers/registrations_controller.rb:11:in `new'

0 个答案:

没有答案