How to improve the checkout so the user doesn't have to re-enter its address on every order?

Just in the first one

Posted almost 6 years ago by Jesus
Posted almost 6 years ago by Alex Yang

Jesus, you'll need to store the user's address information in your User database. To do this, you'll need to do a few things:

1) Add the new fields by creating and running a migration. You should follow the approach used in the 'Add Names to Users' video:

2) In your Order form, you'll need to check to see if the current user already has an address saved. If not, you'll display the specific form fields as is. If so, you'll replace the existing form fields with hidden fields, something like this:

<% f.hidden_field :address, value: current_user.address %>

3) In your Orders controller file, after the Order is placed in the 'create' action, you'll need to save the address information to the User database. Here's some sample code:

current_user.address = @order.address