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
|
# frozen_string_literal: true
class ThingsController < ApplicationController
include Taggable
before_action :set_thing, only: %i[edit show update destroy]
def show; end
def new
@thing = Thing.new
end
def edit; end
def create
ps = thing_params
tag_ids = ps.delete(:tag_ids)
@thing = Thing.new(ps)
@thing.user_id = @current_user.id
updated = ActiveRecord::Base.transaction do
next unless @thing.save
update_tags!(object: @thing, ids: tag_ids)
end
if updated
redirect_to thing_url(@thing.reload), notice: t('things.create-success')
else
render :new, status: :unprocessable_entity
end
end
def update
ps = thing_params
tag_ids = ps.delete(:tag_ids)
updated = ActiveRecord::Base.transaction do
next unless @thing.update(ps)
update_tags!(object: @thing, ids: tag_ids)
end
if updated
redirect_to edit_thing_url(@thing), notice: t('things.update-success')
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@thing.destroy!
redirect_to root_url, notice: t('things.destroy-success')
end
private
def set_thing
@thing = Thing.find(params[:id])
end
def thing_params
params.require(:thing).permit(:target, :title, :publisher, :address, :year, :url, :access,
:authors, :location, :insideof, :pages, :rate, :status, :kind,
:bought_at, :editors, :note, :where_is_it, tag_ids: [])
end
end
|