Class: CategoriesController

Inherits:
ApplicationController show all
Includes:
Pagy::Backend
Defined in:
app/controllers/categories_controller.rb

Overview

This controller manage the views refering to Category model

before_action :ApplicationController#powered_in!

before_action :#set_category, only: [:edit, :update, :destroy]

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_in!, #doctor_in!, #powered_in!, #secretary_in!, #translate_errors

Instance Method Details

#createObject

POST /categories

Create a new Category set @category as new Category with #category_params as param

Returns:

  • (Object)

    render categories/index if @category is created or categories/new if fail



49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/categories_controller.rb', line 49

def create
  @category = Category.new(category_params)
  if @category.save
    categories
    flash.now[:success] = 'Creazione avvenuta con successo'
    render :index, status: :ok
  else
    flash.now[:error] = 'Si è verificato un errore durante la creazione'
    render :new, status: :error
  end
end

#destroyObject

DELETE /categories/1

destroy a category

  • #set_category has set @category for destroy

Returns:

  • (Object)

    render categories/index



84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/categories_controller.rb', line 84

def destroy
  if @category.destroy
    flash.now[:success] = 'Cancellazione avvenuta con successo'
  else
    flash.now[:error] = 'Si è verificato un errore durante la cancellazione'
  end
  render turbo_stream: [
    turbo_stream.replace(:flashes, partial: "flashes"),
    turbo_stream.remove("category_#{@category.id}")
  ]
end

#editObject

GET /categories/1/edit

Render the form for edit a category #set_category has set @category for edit

Returns:

  • (Object)

    render categories/edit



42
# File 'app/controllers/categories_controller.rb', line 42

def edit; end

#indexObject

GET /categories

render categories index set @pagy, @categories for the categorie pagination

Returns:

  • (Object)

    render categories/index



17
# File 'app/controllers/categories_controller.rb', line 17

def index; end

#listObject

GET /categories/list

render the list the all Audit

  • set @pagy, @categories for the @categories pagination

Returns:

  • (Object)

    render categories/index



24
25
26
# File 'app/controllers/categories_controller.rb', line 24

def list
  @pagy, @categories = pagy(categories, link_extra: "data-turbo-frame='categories'")
end

#newObject

GET /categories/new

Render the form for create a new category set @category as new Category

Returns:

  • (Object)

    rendeer categories/new



33
34
35
# File 'app/controllers/categories_controller.rb', line 33

def new
  @category = Category.new
end

#updateObject

PATCH/PUT /categories/1

update a category #set_category has set @category for update and update with #category_params as param

Returns:

  • (Object)

    render categories/index if @category is updated or categories/new if fail



67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/categories_controller.rb', line 67

def update
  if @category.update(category_params)
    categories
    flash.now[:success] = 'Modifica avvenuta con successo'
    render :index, status: :ok
  else
    flash.now[:error] = 'Si è verificato un errore durante la modifica'
    render :edit, status: :error
  end
end