Skip to content

Instantly share code, notes, and snippets.

@profh
Last active May 2, 2019 21:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save profh/51934df52b4e907667a4fc22b5fb1935 to your computer and use it in GitHub Desktop.
class CustomersController < ApplicationController
include ActionView::Helpers::NumberHelper
# Many controller actions missing...
def new
@customer = Customer.new
user = @customer.build_user
end
def edit
# reformat phone w/ dashes when displayed for editing (preference; not required)
@customer.phone = number_to_phone(@customer.phone)
# should have a user associated with customer, but just in case...
user = (@customer.user.nil? ? @customer.build_user : @customer.user)
end
# ... and later, create private methods
private
def convert_user_role
unless current_user && current_user.role?(:admin)
# if you aren't admin, we are overriding the role param to be customer
params[:customer][:user_attributes][:role] = "customer"
end
end
def set_customer
@customer = Customer.find(params[:id])
end
def customer_params
convert_user_role
params.require(:project).permit(:first_name, :last_name, :email, :phone, :active, user_attributes: [:username, :password, :password_confirmation, :role])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment