title: Rails 4: Rendering devise error messages in custom form
body: <p>I have 2 different devise registration forms, one for normal users and another for vendors, well I created a custom registrations controller and it is correctly working:</p>
  <pre><code>class RegistrationsController &lt; Devise::RegistrationsController
      def create
          super
          if resource.save
              if params[:role] == 'vendedor'
                  resource.add_role :vendedor
              end
          end
      end
      
      #In this action I render the custom form for vendors registration
      def new_with_role
          build_resource({})
          case params[:role]
          when 'proveedor'
  
          else
              redirect_to root_path, alert: 'Unexpected error'
          end
      end
  end
  </code></pre>
  <p>I created this route for the custom form for vendors:</p>
  <pre><code>devise_scope :user do
      get &quot;/sign_up/:role&quot; =&gt; 'registrations#new_with_role', as: 'new_user_with_role'
    end
  </code></pre>
  <p>And this is the form <code>views/devise/registrations/new_with_role.html.erb</code></p>
  <pre><code>&lt;%= form_for(resource, as: resource_name, url: registration_path(resource_name), builder: FoundationFormBuilder) do |f| %&gt;
          &lt;%= f.text_field :name, label: &quot;Company name&quot;, autofocus: true %&gt;
          &lt;%= f.email_field :email, label: &quot;Correo electrónico(ideal: Gmail, Hotmail, Outlook, Yahoo)&quot; %&gt;
          &lt;%if @validatable %&gt;
            &lt;%= f.password_field :password, autocomplete: &quot;off&quot;, hint: &quot;#{@minimum_password_length} caracteres como mínimo&quot; %&gt;
          &lt;% else %&gt;
            &lt;%= f.password_field :password, autocomplete: &quot;off&quot; %&gt;
          &lt;% end %&gt;
          &lt;%= f.password_field :password_confirmation, label: &quot;Confirmar contraseña&quot;, autocomplete: &quot;off&quot; %&gt;
          &lt;%= hidden_field_tag :role, 'vendedor' %&gt; #With this custom param I can set the role of the user in the create action of the custom registrations controller
          &lt;%= f.submit &quot;Enviar&quot;, data: { disable_with: &quot;Wait please...&quot; } %&gt;
      &lt;% end %&gt;
  </code></pre>
  <p>The form works great, however I can't figure out how to render the errors in this custom form, When I send the form with intentional errors I get the following:</p>
  <pre><code>undefined method `errors' for nil:NilClass
  </code></pre>
  <p>Checking the logs I could find that when there are errors the <code>create</code> action of my <code>RegistrationsController</code> is rendering the <code>new</code> action of Devise, and I think that is causing the error, I want to render the <code>new_with_role</code> action if there is an error filling its form.</p>
  <p>These are the logs when I send the form for vendors with intentional mistakes:</p>
  <pre><code>Started POST &quot;/users&quot; for ::1 at 2016-02-01 15:33:36 -0500
  Processing by RegistrationsController#create as HTML
    Parameters: {&quot;utf8&quot;=&gt;&quot;✓&quot;, &quot;authenticity_token&quot;=&gt;&quot;kwiV0+GwiGTYVQf0iFE19YoZqZKnAgqzGkTr3D9OCYy03ZPy7xC4ZjFSlnfItE4fuofRAg9jMJ3wIN+si8oR+w==&quot;, &quot;user&quot;=&gt;{&quot;last_name&quot;=&gt;&quot;&quot;, &quot;phone1&quot;=&gt;&quot;&quot;, &quot;first_name&quot;=&gt;&quot;&quot;, &quot;email&quot;=&gt;&quot;&quot;, &quot;preference_details&quot;=&gt;&quot;&quot;, &quot;password&quot;=&gt;&quot;[FILTERED]&quot;, &quot;password_confirmation&quot;=&gt;&quot;[FILTERED]&quot;}, &quot;business_line&quot;=&gt;&quot;2&quot;, &quot;role&quot;=&gt;&quot;vendedor&quot;, &quot;commit&quot;=&gt;&quot;Enviar&quot;}
     (0.4ms)  BEGIN
    User Exists (1.4ms)  SELECT  1 AS one FROM `users` WHERE `users`.`email` = BINARY '' LIMIT 1
     (0.3ms)  ROLLBACK
    Rendered devise/registrations/new.html.erb within layouts/application (84.1ms)
  Completed 500 Internal Server Error in 224ms (ActiveRecord: 2.1ms)
  
  ActionView::Template::Error (undefined method `errors' for nil:NilClass):
      11:     &lt;% end %&gt;
      12:     &lt;h5 class=&quot;subtitle&quot;&gt;O regístrate de la forma tradicional&lt;/h5&gt;
      13:     &lt;%= form_for(:user, as: resource_name, url: registration_path(resource_name), builder: FoundationFormBuilder) do |f| %&gt;
      14:         &lt;%= f.text_field :first_name, label: &quot;Nombre&quot;, autofocus: true %&gt;
      15:         &lt;%= f.text_field :last_name, label: &quot;Apellido&quot; %&gt;
      16:         &lt;%= f.email_field :email, label: &quot;Correo electrónico&quot; %&gt;
      17:         &lt;div class=&quot;row collapse&quot;&gt;
    lib/foundation_form_builder.rb:75:in `errors_on?'
    lib/foundation_form_builder.rb:21:in `block (2 levels) in &lt;class:FoundationFormBuilder&gt;'
    app/views/devise/registrations/new.html.erb:14:in `block in _app_views_devise_registrations_new_html_erb__4160932045667554001_70342326706100'
    app/views/devise/registrations/new.html.erb:13:in `_app_views_devise_registrations_new_html_erb__4160932045667554001_70342326706100'
    app/controllers/registrations_controller.rb:3:in `create'
  
  
    Rendered /Users/sebastianvelandiagiraldo/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (9.4ms)
    Rendered /Users/sebastianvelandiagiraldo/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.2ms)
    Rendered /Users/sebastianvelandiagiraldo/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
    Rendered /Users/sebastianvelandiagiraldo/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (43.8ms)
  </code></pre>
  <p>As you can see when there are errors on the form the rendered action is:</p>
  <pre><code>Rendered devise/registrations/new.html.erb
  </code></pre>
  <p>it should be:</p>
  <pre><code>Rendered devise/registrations/new_with_role.html.erb
  </code></pre>
  <p>I guess that's causing the issue. Any help would be very appreciated</p>
  <h1>Update 1</h1>
  <p>I found <a href="https://stackoverflow.com/questions/20344619/rendering-devise-error-messages-in-scoped-custom-form">this</a> issue that is similar, but the answer provided there is useless.</p>
  <h1>Update 2</h1>
  <p>Code of the create action of my current Devise version 3.5.2 as requested</p>
  <pre><code>def create
    build_resource(sign_up_params)
  
          resource.save
          yield resource if block_given?
          if resource.persisted?
              if resource.active_for_authentication?
                  set_flash_message :notice, :signed_up if is_flashing_format?
                  sign_up(resource_name, resource)
                  respond_with resource, location: after_sign_up_path_for(resource)
              else
                  set_flash_message :notice, :&quot;signed_up_but_#{resource.inactive_message}&quot; if is_flashing_format?
                  expire_data_after_sign_in!
                  respond_with resource, location: after_inactive_sign_up_path_for(resource)
              end
          else
              clean_up_passwords resource
              set_minimum_password_length
              if params[:role].present?
                  render :new_with_role
              else
                  respond_with resource
              end
          end
  end
  </code></pre>