質問編集履歴

1

コードを記述しました。

2019/09/12 08:50

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -78,16 +78,166 @@
78
78
 
79
79
  controller
80
80
 
81
+ ```
82
+
83
+ class BlogsController < ApplicationController
84
+
85
+ def index
86
+
87
+ @books = Book.all
88
+
89
+ @book = Book.new
90
+
91
+ end
92
+
93
+
94
+
95
+ def create
96
+
97
+ book = Book.new(book_params)
98
+
99
+ book.save
100
+
101
+ redirect_to books_path(book.id)
102
+
103
+ end
104
+
105
+
106
+
107
+ def show
108
+
109
+ @book = Book.find(params[:id])
110
+
111
+ end
112
+
113
+
114
+
115
+ def edit
116
+
117
+ @book = Book.find(params[:id])
118
+
119
+ end
120
+
121
+
122
+
123
+ def destroy
124
+
125
+ book = Book.find(params[:id])
126
+
127
+ book.destroy
128
+
129
+ redirect_to books_path
130
+
131
+ end
132
+
133
+
134
+
135
+ private
136
+
137
+
138
+
139
+ def book_params
140
+
81
- ![イメージ説明](c37adb0226852e76ffb8badf98314181.png)
141
+ params.require(:book).permit(:title, :body)
142
+
143
+ end
144
+
145
+ end
146
+
147
+ ```
82
148
 
83
149
 
84
150
 
85
151
  routes
86
152
 
153
+ ```
154
+
87
- ![イメージ説明](008c50a7e78619e8b9aa022f0b7db7cd.png)
155
+ Rails.application.routes.draw do
156
+
157
+ get root 'top#top'
158
+
159
+
160
+
161
+ resources :blogs
162
+
163
+
164
+
165
+ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
166
+
167
+ end
168
+
169
+ ```
88
170
 
89
171
 
90
172
 
91
173
  index.html.erb
92
174
 
175
+ ```
176
+
177
+ <h1>Books</h1>
178
+
179
+
180
+
181
+ <table>
182
+
183
+ <thead>
184
+
185
+ <tr>
186
+
187
+ <th>Title</th>
188
+
189
+ <th>Body</th>
190
+
93
- ![イメージ説明](c3e181481a35abf207f70d73647108ba.png)
191
+ <th colspan="2"></th>
192
+
193
+ </tr>
194
+
195
+ </thead>
196
+
197
+
198
+
199
+ <tbody>
200
+
201
+ <% @books.each do |book| %>
202
+
203
+ <tr>
204
+
205
+ <td><%= book.title %></td>
206
+
207
+ <td><%= book.body %></td>
208
+
209
+ <td><%= link_to 'Show', blog_path %></td>
210
+
211
+ <td><%= link_to 'Edit', edit_book_path(book.id) %></td>
212
+
213
+ <td><%= link_to 'Destroy', blogs_path(book.id), method: :delete, "data-confirm" => "本当に削除しますか?" %></td>
214
+
215
+ </tr>
216
+
217
+ <% end %>
218
+
219
+ </tbody>
220
+
221
+ </table>
222
+
223
+
224
+
225
+ <h1>New book</h1>
226
+
227
+
228
+
229
+ <%= form_for(@book) do |f| %>
230
+
231
+ <h4>Title</h4>
232
+
233
+ <%= f.text_field :title %>
234
+
235
+ <h4>Body</h4>
236
+
237
+ <%= f.text_area :body %>
238
+
239
+ <%= f.submit 'Create Book' %>
240
+
241
+ <% end %>
242
+
243
+ ```