3

I'm having issues loading a new registration page using Devise in my rails app. When I try to load the sign up page, I get the error:

undefined method `errors' for nil:NilClass

which seems to be caused by the following line:

 <%= devise_error_messages! %>

What could I be doing wrong?

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

The sign up link:

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

application_helper.rb (where resource_name is defined)

def resource_name
    :user
end

def resource
    @resource ||= User.new
end

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

the sign up view

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

This is the full error message:

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'
scientiffic
  • 9,045
  • 18
  • 76
  • 149
  • What is `resource_name` in `new_registration_path(resource_name)`? Where is it defined? – Kirti Thorat Feb 27 '14 at 22:29
  • Also, could you please share the sign_up view. – Kirti Thorat Feb 27 '14 at 22:36
  • I edited my question to include my application_helper.rb, which defines resource_name, and the sign_up view. – scientiffic Feb 28 '14 at 00:11
  • Are you `Sign Up and Sign In` on same page? – Kirti Thorat Feb 28 '14 at 00:25
  • the sign in is on a different page – scientiffic Feb 28 '14 at 00:36
  • Add this to semantic_form_for arguments `, namespace: 'sign_up', html: { id: 'form_user_sign_up' }` and check if it works – Kirti Thorat Feb 28 '14 at 00:39
  • thanks for your help. unfortunately, I still get the same error even after updating my form with your suggestion: <%= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), namespace: 'sign_up', html: {id: 'form_user_sign_up'}) do |f| %> – scientiffic Feb 28 '14 at 16:43
  • Try `semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), namespace: 'sign_up', html: {id: 'form_user_sign_up'}) do |f| %>` – Kirti Thorat Feb 28 '14 at 16:43
  • namespace should be outside – Kirti Thorat Feb 28 '14 at 16:44
  • Can you share the error stacktrace in question. – Kirti Thorat Feb 28 '14 at 16:48
  • sorry, I'm not seeing how what you shared is different than mine. I copy and pasted what you had but it still didn't work. I added the full error message to my question. – scientiffic Feb 28 '14 at 16:49
  • Thats because you edited the comment, in the previous version namespace was inside the registration_path. By that time i already posted the change. anyways could you share the stacktrace. – Kirti Thorat Feb 28 '14 at 16:52
  • I think you have nil resource (as it probably should be) and this causes the exception. Maybe this reading will help you: http://stackoverflow.com/questions/4101641/rails-devise-handling-devise-error-messages – Nikolai Apr 30 '15 at 11:47

0 Answers0