Devise registration for with nested entity

o..o

I'm building a Rails app where User can have more Addresses.

User has_many :addresses
Address belong_to :user

I'm using Devise for authentication. I want an User entity and first Address entity to be created by one form when the user is registering.

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= f.email_field :email %><br />
  <%= t.password_field :password %><br />

  <%= f.fields_for resource.addresses do |a| %>
    <%= a.text_field :street %>
  <% end %>
<% end %>

But I'm getting

undefined method 'street' for ActiveRecord::Associations::CollectionProxy []

what must be done in controller?

Thanks

EDIT

I have already in the User model:

accepts_nested_attributes_for :addresses

And I have updated my controller like this:

class Users::RegistrationsController < Devise::RegistrationsController

  # GET /resource/sign_up
  def new
    # super
    build_resource({})
    yield resource if block_given?
    resource.addresses.build
    respond_with resource
  end
end

and view:

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= f.email_field :email %><br />
  <%= t.password_field :password %><br />

  <%= f.fields_for resource.addresses.first do |a| %>
  <%= a.text_field :street %>
<% end %>

So the form is displaying. But when I post, the resource.addresses.first is still null:

undefined method `model_name' for nil:NilClass

Thanks

Bala Karthik

You need to add accepts_nested_attributes_for addresses

Class User < ActiveRecord::Base
  has_many :addresses
  accepts_nested_attributes_for :addresses
end

You also need to initialise the address object, You can do this at controller level (Best Approach) or at view

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= f.email_field :email %><br />
  <%= t.password_field :password %><br />
  <% resource.addresses.build %>
  <%= f.fields_for resource.addresses do |a| %>
    <%= a.text_field :street %>
  <% end %>
<% end %>

And you need to add the corresponding to the parameters that you are receiving at your controller.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Translate devise labels in registration?

From Dev

Set user_id for nested_attribute in devise registration before user exists

From Dev

Creating an alternative registration action in Devise

From Dev

Devise + RSpec - Validation Errors for Registration

From Dev

Devise registration submitting form twice?

From Dev

Have Devise create a subdomain on registration

From Dev

Creating an empty profile on devise registration

From Dev

Devise registration submitting form twice?

From Dev

Devise + RSpec - Validation Errors for Registration

From Dev

Creating an alternative registration action in Devise

From Dev

Devise gem registration model issue

From Dev

nested resources in devise

From Dev

Additional Processing Required in Registration for a Devise Login System

From Dev

how to create a model after a devise registration

From Dev

Sign in inherited user after registration with Devise

From Dev

Log a user into their subdomain after registration with Rails and Devise

From Dev

Rails - Devise / User registration route / POST

From Dev

Ruby on Rails Devise Edit Registration Routing Error

From Dev

Devise User registration validation based on role

From Dev

ActiveModel::ForbiddenAttributesError while using devise for registration

From Dev

Create another model upon Devise User registration

From Dev

Need to set an association on a User with Devise registration

From Dev

How to generate Registration controller of devise gem

From Dev

Log a user into their subdomain after registration with Rails and Devise

From Dev

Rails, Devise remove registration of specific model

From Dev

Customising Devise registration edit/update routes and views

From Dev

Devise - Allow Admin to Add Registration Accounts

From Dev

Devise custom validation for a custom field in the registration form

From Dev

Passing arbitrary parameters on user registration with Devise

Related Related

  1. 1

    Translate devise labels in registration?

  2. 2

    Set user_id for nested_attribute in devise registration before user exists

  3. 3

    Creating an alternative registration action in Devise

  4. 4

    Devise + RSpec - Validation Errors for Registration

  5. 5

    Devise registration submitting form twice?

  6. 6

    Have Devise create a subdomain on registration

  7. 7

    Creating an empty profile on devise registration

  8. 8

    Devise registration submitting form twice?

  9. 9

    Devise + RSpec - Validation Errors for Registration

  10. 10

    Creating an alternative registration action in Devise

  11. 11

    Devise gem registration model issue

  12. 12

    nested resources in devise

  13. 13

    Additional Processing Required in Registration for a Devise Login System

  14. 14

    how to create a model after a devise registration

  15. 15

    Sign in inherited user after registration with Devise

  16. 16

    Log a user into their subdomain after registration with Rails and Devise

  17. 17

    Rails - Devise / User registration route / POST

  18. 18

    Ruby on Rails Devise Edit Registration Routing Error

  19. 19

    Devise User registration validation based on role

  20. 20

    ActiveModel::ForbiddenAttributesError while using devise for registration

  21. 21

    Create another model upon Devise User registration

  22. 22

    Need to set an association on a User with Devise registration

  23. 23

    How to generate Registration controller of devise gem

  24. 24

    Log a user into their subdomain after registration with Rails and Devise

  25. 25

    Rails, Devise remove registration of specific model

  26. 26

    Customising Devise registration edit/update routes and views

  27. 27

    Devise - Allow Admin to Add Registration Accounts

  28. 28

    Devise custom validation for a custom field in the registration form

  29. 29

    Passing arbitrary parameters on user registration with Devise

HotTag

Archive