Last active
May 2, 2019 21:04
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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