0
votes

I'm trying to create a simple User & Address Model.

I've created a nested form that has an address model nested into the User model. My User Model has the devise attributes etc.

It all works fine, except that there's a strange behaviour happening when the user is created.

If you read below, you'll notice the "user_id" which is the address of the owning user is populated when the record is first committed. But then immediately afterward, something that looks like it's in Devise core, is resetting the user_id to null.

Does anyone know what would be causing this?

Started POST "/users" for 127.0.0.1 at 2012-11-03 22:45:20 +1100 Processing by Member::UsersController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"Pr8lSEJalWwosuaLPOrYqQ+sOkFCyoNujaUybxpZDAg=", "user"=>{"address_attributes"=>{"first_name"=>"HOlley", "last_name"=>"Hou"}, "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}} Creating a new user (0.1ms) BEGIN (0.1ms) COMMIT (0.0ms) BEGIN Member::User Exists (0.3ms) SELECT 1 AS one FROM member_users WHERE member_users.email = BINARY '[email protected]' LIMIT 1 SQL (0.2ms) INSERT INTO member_users (created_at, current_sign_in_at, current_sign_in_ip, email, encrypted_password, last_sign_in_at, last_sign_in_ip, remember_created_at, reset_password_sent_at, reset_password_token, sign_in_count, updated_at) VALUES ('2012-11-03 11:45:20', NULL, NULL, '[email protected]', '$2a$10$1JvC/sO85L2esNp8xA9qseTc78.rtOyF3zSBi/fOTiWJRt9.zgYha', NULL, NULL, NULL, NULL, NULL, 0, '2012-11-03 11:45:20') SQL (0.2ms) INSERT INTO member_addresses (address3, address_1, address_2, address_type, business_name, city, country, created_at, first_name, geo_late, geo_long, is_primary, last_name, middle_name, phone1, phone1_type, phone2, phone2_type, postcode, primary_email, secondary_email, state, status, suburb, title, updated_at, user_id) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2012-11-03 11:45:20', 'HOlley', NULL, NULL, NULL, 'Hou', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2012-11-03 11:45:20', 5) (1.3ms) COMMIT
(0.1ms) BEGIN (0.2ms) UPDATE member_users SET last_sign_in_at = '2012-11-03 11:45:20', current_sign_in_at = '2012-11-03 11:45:20', last_sign_in_ip = '127.0.0.1', current_sign_in_ip = '127.0.0.1', sign_in_count = 1, updated_at = '2012-11-03 11:45:20' WHERE member_users.id = 5 (0.2ms) COMMIT (0.1ms) BEGIN
(0.2ms) UPDATE member_addresses SET user_id = NULL, updated_at = '2012-11-03 11:45:20' WHERE member_addresses.id = 5 (0.2ms) COMMIT

The controller is a custom controller because I eventually (once I get this working), need to be able to redirect to 2 different places after registration depending where the customer logged in from (checkout or header in this case).

class Member::UsersController < Devise::RegistrationsController
  def create
    super
  end

  def new
    super
  end

  def edit
    Rails.logger.debug { "Editing the user" }
    super
  end

  def show
    Rails.logger.debug { "Showing the user controller" }
  end
end

and from routes.rb

  devise_for :users, :class_name => "Member::User", :controllers => {:registrations => 'member/users', :sessions => 'member/sessions'}
1
Are you using a custom controller or the basic one that devise provides ?pjam
Custom controller, but the controller at this stage is just calling "super". I'll post it on the thread.Richard G

1 Answers

0
votes

I finally figured this out. It was actually related to an answer I got from a previous question regarding nested form.

In this code below, previously in the nested address field, I was always calling build_address and because address is one to one relationship when the form was refreshing it was creating a new address. Now I think it should be ok, I haven't tested it, but at least the address nullification problem is resolved.

resource_name,:url => registration_path(resource_name), :html => {:class => 'form-horizontal' }) do |f| %> Mandatory fields marked *

"alert alert-error", :id => "flash_error") if flash[:error] %> "flash_notice") if flash[:notice] %> "alert alert-error", :id => "flash_alert") if flash[:alert] %> {:class => "input-xlarge"} %> {:class => "input-xlarge"} %>
        <%= f.input :email, :input_html => {:class => "input-xlarge"} %>
        <%= f.input :password, :required => true, :input_html => {:class => "input-xlarge"}   %>
        <%= f.input :password_confirmation, :required => true, :input_html => {:class => "input-xlarge"} %>
        <div class="form-actions">
          <%= f.button :submit, "Register", :class => "btn btn-success btn-large" %>
        </div>
    <% end %>