質問編集履歴

1

ソースコード追加

2018/08/16 09:22

投稿

nobu09
nobu09

スコア34

test CHANGED
File without changes
test CHANGED
@@ -14,9 +14,265 @@
14
14
 
15
15
  ### 該当のソースコード
16
16
 
17
+ post.js.coffee
18
+
19
+ ```
20
+
21
+ $(document).ready ->
22
+
23
+ $("#posts .page").infinitescroll
24
+
25
+ navSelector: "nav.pagination" # selector for the paged navigation (it will be hidden)
26
+
27
+ nextSelector: "nav.pagination a[rel=next]" # selector for the NEXT link (to page 2)
28
+
29
+ itemSelector: "#posts tr.post" # selector for all items you'll retrieve
30
+
31
+ ```
32
+
33
+
34
+
35
+ posts/indx.html.erb
36
+
37
+ ```
38
+
39
+ <p id="notice"><%= notice %></p>
40
+
41
+
42
+
43
+ <h1>Listing Posts</h1>
44
+
45
+
46
+
47
+ <table id="posts">
48
+
17
- gitにアップしています。
49
+ <thead>
50
+
18
-
51
+ <tr>
52
+
53
+ <th>Title</th>
54
+
55
+ <th>Author</th>
56
+
57
+ <th>Body</th>
58
+
59
+ <th colspan="3"></th>
60
+
61
+ </tr>
62
+
63
+ </thead>
64
+
65
+
66
+
67
+ <tbody class="page">
68
+
69
+ <%= render @posts %>
70
+
71
+ </tbody>
72
+
73
+ </table>
74
+
75
+
76
+
77
+ <br>
78
+
79
+
80
+
81
+ <%= link_to 'New Post', new_post_path %>
82
+
83
+ <%= paginate @posts %>
84
+
85
+ ```
86
+
87
+
88
+
89
+ _post.html.erb
90
+
91
+ ```
92
+
93
+ <tr class="post">
94
+
95
+ <td><%= post.title %></td>
96
+
97
+ <td><%= post.author %></td>
98
+
99
+ <td><%= post.body %></td>
100
+
101
+ <td><%= link_to 'Show', post %></td>
102
+
103
+ <td><%= link_to 'Edit', edit_post_path(post) %></td>
104
+
105
+ <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
106
+
107
+ </tr>
108
+
109
+ ```
110
+
111
+
112
+
113
+ index.js.erb
114
+
115
+ ```
116
+
117
+ $("#posts").append("<tbody class='page'><%= escape_javascript(render(@posts)) %></tbody>");
118
+
119
+ ```
120
+
121
+
122
+
123
+ posts_controller.rb
124
+
125
+ ```
126
+
127
+ class PostsController < ApplicationController
128
+
129
+ before_action :set_post, only: [:show, :edit, :update, :destroy]
130
+
131
+
132
+
133
+ # GET /posts
134
+
135
+ # GET /posts.json
136
+
137
+ def index
138
+
139
+ @posts = Post.order(:created_at).page(params[:page])
140
+
141
+ end
142
+
143
+
144
+
145
+ # GET /posts/1
146
+
147
+ # GET /posts/1.json
148
+
149
+ def show
150
+
151
+ end
152
+
153
+
154
+
155
+ # GET /posts/new
156
+
157
+ def new
158
+
159
+ @post = Post.new
160
+
161
+ end
162
+
163
+
164
+
165
+ # GET /posts/1/edit
166
+
167
+ def edit
168
+
169
+ end
170
+
171
+
172
+
173
+ # POST /posts
174
+
175
+ # POST /posts.json
176
+
177
+ def create
178
+
179
+ @post = Post.new(post_params)
180
+
181
+
182
+
183
+ respond_to do |format|
184
+
185
+ if @post.save
186
+
187
+ format.html { redirect_to @post, notice: 'Post was successfully created.' }
188
+
189
+ format.json { render :show, status: :created, location: @post }
190
+
191
+ else
192
+
193
+ format.html { render :new }
194
+
195
+ format.json { render json: @post.errors, status: :unprocessable_entity }
196
+
197
+ end
198
+
199
+ end
200
+
201
+ end
202
+
203
+
204
+
205
+ # PATCH/PUT /posts/1
206
+
19
- https://github.com/NMurata07/endless_kaminari
207
+ # PATCH/PUT /posts/1.json
208
+
209
+ def update
210
+
211
+ respond_to do |format|
212
+
213
+ if @post.update(post_params)
214
+
215
+ format.html { redirect_to @post, notice: 'Post was successfully updated.' }
216
+
217
+ format.json { render :show, status: :ok, location: @post }
218
+
219
+ else
220
+
221
+ format.html { render :edit }
222
+
223
+ format.json { render json: @post.errors, status: :unprocessable_entity }
224
+
225
+ end
226
+
227
+ end
228
+
229
+ end
230
+
231
+
232
+
233
+ # DELETE /posts/1
234
+
235
+ # DELETE /posts/1.json
236
+
237
+ def destroy
238
+
239
+ @post.destroy
240
+
241
+ respond_to do |format|
242
+
243
+ format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
244
+
245
+ format.json { head :no_content }
246
+
247
+ end
248
+
249
+ end
250
+
251
+
252
+
253
+ private
254
+
255
+ # Use callbacks to share common setup or constraints between actions.
256
+
257
+ def set_post
258
+
259
+ @post = Post.find(params[:id])
260
+
261
+ end
262
+
263
+
264
+
265
+ # Never trust parameters from the scary internet, only allow the white list through.
266
+
267
+ def post_params
268
+
269
+ params.require(:post).permit(:title, :author, :body)
270
+
271
+ end
272
+
273
+ end
274
+
275
+ ```
20
276
 
21
277
 
22
278