HER- 66 creating address controller #58
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
HER-66 Creating Address Controller
Issue
https://raquelanaroman.atlassian.net/browse/HER-66
Description
Enhance the address management system to allow users to create and edit addresses for themselves and their organization in their profile.
Acceptance Criteria
Users can create and edit addresses in their profiles for themselves and their organization.
Implementation
Generate a migration to add a save_to_user attribute to the Address model.
rails generate migration AddSaveToUserToAddresses save_to_user:boolean
rails db:migrate
Ensure the Address model includes the save_to_user attribute.
attribute :save_to_user, :boolean, default: false
Generate the Addresses controller.
rails generate controller Addresses
Set up routes to nest addresses under users and organizations.
resources :users do
resources :addresses, only: [ :create, :update, :destroy ]
end
Implement action for creating, updating, and destroying addresses.
class Api::V1::AddressesController < ApplicationController
before_action :set_addressable
def create
@address = @addressable.addresses.build(address_params)
if @address.save
render json: @address, status: :created
else
render json: @address.errors, status: :unprocessable_entity
end
end
def update
@address = @addressable.addresses.find(params[:id])
if @address.update(address_params)
render json: @address, status: :ok
else
render json: @address.errors, status: :unprocessable_entity
end
end
def destroy
@address = @addressable.addresses.find(params[:id])
@address.destroy
head :no_content
end
private
def set_addressable
@Addressable = if params[:user_id]
User.find(params[:user_id])
elsif params[:organization_id]
Organization.find(params[:organization_id])
end
end
def address_params
params.require(:address).permit(:street_address, :city, :state, :postal_code, :save_to_user)
end
end
Linked work items
#HER-71
Changes
Review Checklist