impress/app/controllers/closet_lists_controller.rb

56 lines
1.3 KiB
Ruby
Raw Permalink Normal View History

class ClosetListsController < ApplicationController
before_action :authorize_user!
before_action :find_closet_list, :only => [:edit, :update, :destroy]
def create
2023-07-29 11:22:15 -07:00
@closet_list = current_user.closet_lists.build closet_list_params
if @closet_list.save
2011-07-29 07:52:04 -07:00
save_successful!
else
2011-07-29 07:52:04 -07:00
save_failed!
render action: :new, status: :unprocessable_entity
end
end
2011-07-29 07:52:04 -07:00
def destroy
@closet_list.destroy
flash[:notice] = "Successfully deleted \"#{@closet_list.name}\""
2011-07-29 07:52:04 -07:00
redirect_to user_closet_hangers_path(current_user)
end
def new
2023-07-29 11:22:15 -07:00
@closet_list = current_user.closet_lists.build closet_list_params
2011-07-29 07:52:04 -07:00
end
def update
@closet_list.attributes = closet_list_params
if @closet_list.save
2011-07-29 07:52:04 -07:00
save_successful!
else
save_failed!
render action: :edit, status: :unprocessable_entity
2011-07-29 07:52:04 -07:00
end
end
protected
2023-07-29 11:22:15 -07:00
def closet_list_params
params.require(:closet_list).permit(
:description, :hangers_owned, :name, :visibility)
end
2011-07-29 07:52:04 -07:00
def find_closet_list
@closet_list = current_user.closet_lists.find params[:id]
end
def save_failed!
flash.now[:alert] = "We can't save this list because: #{@closet_list.errors.full_messages.to_sentence}"
end
def save_successful!
flash[:notice] = "Successfully saved \"#{@closet_list.name}\""
2011-07-29 07:52:04 -07:00
redirect_to user_closet_hangers_path(current_user)
end
end