質問編集履歴

1

コントローラーとビューのコードを追加しました。

2021/10/01 09:15

投稿

TakaakiITO
TakaakiITO

スコア3

test CHANGED
File without changes
test CHANGED
@@ -28,8 +28,166 @@
28
28
 
29
29
  app/views/coffees/show.html.erb:9
30
30
 
31
+
32
+
31
33
  ```
32
34
 
35
+ <h1>Coffee詳細</h1>
36
+
37
+ <div class="coffee">
38
+
39
+ <p><%= @coffee.name %></p>
40
+
41
+ <p><%= @coffee.created_at %></p>
42
+
43
+ <p><%= image_tag @coffee.image_url, size: "250x200" if @coffee.image? %></p>
44
+
45
+
46
+
47
+ </div>
48
+
49
+ <h1>users#show</h1>
50
+
51
+ <p>名前 : <%= @user.name %></p>
52
+
53
+ <p>メールアドレス : <%= @user.email %></p>
54
+
55
+ <p>プロフィール : <%= @user.profile %></p>
56
+
57
+
58
+
59
+ <% if current_user.id == @user.id %>
60
+
61
+ <%= link_to "編集する", edit_user_registration_path %>
62
+
63
+ <% end %>
64
+
65
+
66
+
67
+ <h2>ユーザーの投稿一覧</h2>
68
+
69
+ <% @user.coffees.each do |t| %>
70
+
71
+ <%= t.user.name %>
72
+
73
+ <%= t.body %>
74
+
75
+ <% end %>
76
+
77
+ <%= link_to "編集する", edit_coffee_path(@coffee.id) %>
78
+
79
+ <%= link_to "Coffee一覧に戻る", coffees_path %>
80
+
81
+
82
+
83
+ class CoffeesController < ApplicationController
84
+
85
+ before_action :authenticate_user!, only: [:new, :create]
86
+
87
+ def index
88
+
89
+ @coffees=Coffee.all
90
+
91
+ end
92
+
93
+
94
+
95
+
96
+
97
+ def new
98
+
99
+ @coffee = Coffee.new
100
+
101
+ end
102
+
103
+
104
+
105
+
106
+
107
+ def create
108
+
109
+ coffee = Coffee.new(coffee_params)
110
+
111
+ coffee.user_id = current_user.id
112
+
113
+ if coffee.save
114
+
115
+ redirect_to :action => "index"
116
+
117
+ else
118
+
119
+ redirect_to :action => "new"
120
+
121
+ end
122
+
123
+ end
124
+
125
+
126
+
127
+ def show
128
+
129
+ @coffee = Coffee.find(params[:id])
130
+
131
+ end
132
+
133
+
134
+
135
+ def edit
136
+
137
+ @coffee =Coffee.find(params[:id])
138
+
139
+ end
140
+
141
+
142
+
143
+
144
+
145
+ def update
146
+
147
+ coffee = Coffee.find(params[:id])
148
+
149
+ if coffee.update(coffee_params)
150
+
151
+ redirect_to :action => "show", :id => coffee.id
152
+
153
+ else
154
+
155
+ redirect_to :action => "new"
156
+
157
+ end
158
+
159
+ end
160
+
161
+
162
+
163
+
164
+
165
+ def destroy
166
+
167
+ coffee = Coffee.find(params[:id])
168
+
169
+ coffee.destroy
170
+
171
+ redirect_to action: :index
172
+
173
+ end
174
+
175
+
176
+
177
+ private
178
+
179
+ def coffee_params
180
+
181
+ params.require(:coffee).permit(:name, :address, :telephone, :seat, :wifi, :smoke, :image)
182
+
183
+ end
184
+
185
+
186
+
187
+
188
+
189
+ end
190
+
33
191
 
34
192
 
35
193
  ActiveRecord::Schema.define(version: 2021_09_27_150000) do