Created
March 25, 2016 09:07
-
-
Save goozler/c4e3bd395360a5d7ba82 to your computer and use it in GitHub Desktop.
Create user with profile and role
This file contains hidden or 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
.row | |
.col-xs-5 | |
= simple_form_for(@manager, url: @url, as: :manager) do |f| | |
.form-inputs | |
= f.input :username | |
= f.input :email | |
= f.input :password, hint: password_hint | |
= f.input :password_confirmation | |
= simple_fields_for @manager.profile do |p| | |
= p.input :last_name | |
= p.input :first_name | |
= p.input :middle_name | |
= f.association :filial | |
.form-actions | |
= f.button :submit, 'Save', class: 'btn-primary' |
This file contains hidden or 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 ManagersController < ApplicationController | |
def index | |
authorize! :index, User.new(roles: [Role.find_by_name(:manager)]) | |
@managers = User.with_role(:manager) | |
end | |
def show | |
@manager = User.with_role(:manager).find(params[:id]) | |
authorize! :show, @manager | |
end | |
def new | |
@manager = User.new | |
@manager.add_role(:manager) | |
@manager.build_profile | |
authorize! :new, @manager | |
@url = managers_path | |
end | |
def edit | |
@manager = User.with_role(:manager).find(params[:id]) | |
authorize! :edit, @manager | |
@url = manager_path(@manager) | |
end | |
def create | |
@manager = User.new(manager_params) | |
@manager.add_role(:manager) | |
authorize! :create, @manager | |
@manager.build_profile(profile_params) | |
respond_to do |format| | |
if @manager.save | |
format.html do | |
redirect_to manager_path(@manager), notice: 'Manager created' | |
end | |
format.json { render :show, status: :created, location: @manager } | |
else | |
@manager.clean_up_passwords | |
@url = managers_path | |
format.html { render :new } | |
format.json do | |
render json: @manager.errors, status: :unprocessable_entity | |
end | |
end | |
end | |
end | |
def update | |
@manager = User.with_role(:manager).find(params[:id]) | |
authorize! :update, @manager | |
if params[:manager][:password].blank? | |
params[:manager].delete(:password) | |
if params[:password_confirmation].blank? | |
params[:manager].delete(:password_confirmation) | |
end | |
end | |
@manager.assign_attributes(manager_params) | |
@manager.profile.assign_attributes(profile_params) | |
respond_to do |format| | |
if @manager.save | |
format.html do | |
redirect_to manager_path(@manager), notice: 'Manager updated' | |
end | |
format.json { render :show, status: :ok, location: @manager } | |
else | |
@manager.clean_up_passwords | |
@url = manager_path(@manager) | |
format.html { render :edit } | |
format.json do | |
render json: @manager.errors, status: :unprocessable_entity | |
end | |
end | |
end | |
end | |
def destroy | |
@manager = User.with_role(:manager).find(params[:id]) | |
authorize! :destroy, @manager | |
@manager.destroy | |
respond_to do |format| | |
format.html { redirect_to managers_url, notice: 'Manager deleted' } | |
format.json { head :no_content } | |
end | |
end | |
private | |
def manager_params | |
params.require(:manager).permit( | |
:username, :email, :password, :password_confirmation, :filial_id | |
) | |
end | |
def profile_params | |
params.require(:user_profile).permit(:last_name, :first_name, :middle_name) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment