This repository was archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathproducts_controller.rb
More file actions
105 lines (84 loc) · 2.84 KB
/
Copy pathproducts_controller.rb
File metadata and controls
105 lines (84 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# frozen_string_literal: true
module Kit
class ProductsController < ApplicationController
before_action :load_kit_products, only: %i[index show new]
before_action :set_kit_product, only: %i[show update destroy]
# GET /kit/products or /kit/products.json
def index
@kit_product = Kit::Product.new
end
# GET /kit/products/1 or /kit/products/1.json
def show
respond_to do |format|
format.html { render(turbo_frame_request? ? :show : :index) }
format.json { render(:show) }
end
end
# GET /kit/products/new
def new
@kit_product = Kit::Product.new
respond_to do |format|
format.html { render(turbo_frame_request? ? :show : :index) }
format.turbo_stream { render(:show) }
end
end
# POST /kit/products or /kit/products.json
def create
@kit_product = Kit::Product.new(kit_product_params)
respond_to do |format|
if @kit_product.save
flash.now[:notice] = 'Product was successfully created.'
format.html { redirect_to(@kit_product) }
format.json { render(:show, status: :created, location: @kit_product) }
else
format.html do
load_kit_products
render(:index, status: :unprocessable_entity)
end
format.json { render(json: @kit_product.errors, status: :unprocessable_entity) }
end
format.turbo_stream
end
end
# PATCH/PUT /kit/products/1 or /kit/products/1.json
def update
respond_to do |format|
if @kit_product.update(kit_product_params)
flash.now[:notice] = 'Product was successfully updated.'
format.html { redirect_to(@kit_product) }
format.json { render(:show, status: :ok, location: @kit_product) }
else
format.html do
load_kit_products
render(:index, status: :unprocessable_entity)
end
format.json { render(json: @kit_product.errors, status: :unprocessable_entity) }
end
format.turbo_stream
end
end
# DELETE /kit/products/1 or /kit/products/1.json
def destroy
@kit_product.destroy
flash.now[:notice] = 'Product was successfully destroyed.'
respond_to do |format|
format.html { redirect_to(kit_products_url) }
format.json { head(:no_content) }
format.turbo_stream
end
end
private
def load_kit_products
@q = Kit::Product.select(:id, :name).ransack(params[:q])
@pagy, @kit_products = pagy(@q.result)
end
# Use callbacks to share common setup or constraints between actions.
def set_kit_product
@kit_product = Kit::Product.find(params[:id])
end
# Only allow a list of trusted parameters through.
def kit_product_params
params.require(:kit_product).permit(:name)
end
end
end