MrPizzaFace
MrPizzaFace

Reputation: 8086

How to properly split devise registration form to a partial I can use in separate controller?

Issue I am having is that data is not being committed to db. Log shows it rolling back. Don't understand why. I created a partial of the new.html.erb user registration file that I would like to render in my homepage.

Simple_form devise partial as follows:

<h2>Sign up</h2>

<%= simple_form_for(:users, url: :user_registration) do |f| %>
  <%= f.error_notification %>

  <div class="form-group" style="width: 550px;">
    <div class="row top-buffer">
      <div class="col-sm-6">
        <%= f.input :email, required: true, placeholder: "Email" %>
      </div>
    </div>
    <div class="row top-buffer">
      <div class="col-sm-6">
        <%= f.input :password, required: true, placeholder: "New Password" %>
      </div>
      <div class="col-sm-6">
        <%= f.input :password_confirmation, required: true, placeholder: "Re-type Password" %>
      </div>
    </div>
    <div class="row top-buffer">
      <div class="col-sm-3">
        <div class="form-actions"><%= f.button :submit, "Sign up", class: "btn btn-default" %></div>
      </div>
    </div>
  </div>
<% end %>

Development log:

Started POST "/users" for 127.0.0.1 at 2014-06-22 22:55:11 -0400
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"0ecPzs9+1ZRbXitlKaXSmb+yiH9dZ9JWXmXdbAnh81M=", "users"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
   (0.2ms)  BEGIN
   (0.2ms)  ROLLBACK
  Rendered devise/shared/_links.erb (0.6ms)
  Rendered devise/registrations/new.html.erb within layouts/application (29.7ms)
Completed 200 OK in 57ms (Views: 51.0ms | ActiveRecord: 0.4ms)

FWIW - From the users controller I can create a user without issue.

Upvotes: 0

Views: 254

Answers (2)

Richard Peck
Richard Peck

Reputation: 76784

Did you apply the devise resource helper:

#app/helpers/application_helper.rb
Class ApplicationHelper
  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

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

This will allow you to call the form wherever you need. Seems like you've done this already, but I think you've made your code pretty restrictive (have you apply to a particular controller / method)

Upvotes: 0

MrPizzaFace
MrPizzaFace

Reputation: 8086

The solution was to modify the controller and simple_form arguments as follows:

<h2>Sign up</h2>

  <%= simple_form_for(@resource, :url => user_registration_path(@resource)) do |f| %>

    <%= f.error_notification %>
    . . .

And in the controller add:

@resource = User.new

Upvotes: 1

Related Questions