Search code examples
ruby-on-railsrubydevisemarketplace

Devise adding User shipping address


I'm creating a marketplace. I have user accounts handled by devise. I'd like to add extra user attributes. Such as billing and shipping address (and a primary address that takes care of both for most cases).

Devise is configured using default settings. I have used this tool to generate devise controllers in my app controllers folder under the users folder. As I found quite a few posts with people trying to render a form from a different controller/view.

I generated user_address

class CreateUserAddresses < ActiveRecord::Migration
  def change
  create_table :user_addresses do |t|
    t.string :address
    t.string :city
    t.string :state
    t.integer :user_id
  
    t.timestamps null: false
  end
 end
end

And also added a primary address id to my users profile to be used later.

class AddPrimraryAddressIdToUsers < ActiveRecord::Migration
  def change
    add_column :users, :primary_address_id, :integer
  end
end

Now I basically want to integrate this user_address _form.html.erb into my devise user.

./app/views/user_addresses/_form.html.erb

./app/views/devise/registrations/edit.html.erb

<div>
<% if current_user.primary_address_id.blank? %>
   Please create a primary address
      <%= render :partial => '/user_addresses/form'%>
<% end %>
</div>

Which creates and error on the edit page.

First argument in form cannot contain nil or be empty

<%= form_for(@user_address) do |f| %>
  <% if @user_address.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user_address.errors.count, "error") %> prohibited this user_address from being saved:</h2>

  <ul>

Which my understanding is because user_address is not in the devise user controller. How would I integrate everything that user_address created into the devise views and controller to clean up the directory. Or simply render the user_address form.


Solution

  • Is there more functionality in Useraddresses going forward? Or is the sole functionality to add attributes to the User Class. If so, then checkout the post here