teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

ソースコード追加

2018/08/16 09:22

投稿

nobu09
nobu09

スコア35

title CHANGED
File without changes
body CHANGED
@@ -6,9 +6,137 @@
6
6
  エラーメッセージ等は起きていません。
7
7
 
8
8
  ### 該当のソースコード
9
- gitにアップしています。
9
+ post.js.coffee
10
+ ```
11
+ $(document).ready ->
12
+ $("#posts .page").infinitescroll
13
+ navSelector: "nav.pagination" # selector for the paged navigation (it will be hidden)
14
+ nextSelector: "nav.pagination a[rel=next]" # selector for the NEXT link (to page 2)
10
- https://github.com/NMurata07/endless_kaminari
15
+ itemSelector: "#posts tr.post" # selector for all items you'll retrieve
16
+ ```
11
17
 
18
+ posts/indx.html.erb
19
+ ```
20
+ <p id="notice"><%= notice %></p>
21
+
22
+ <h1>Listing Posts</h1>
23
+
24
+ <table id="posts">
25
+ <thead>
26
+ <tr>
27
+ <th>Title</th>
28
+ <th>Author</th>
29
+ <th>Body</th>
30
+ <th colspan="3"></th>
31
+ </tr>
32
+ </thead>
33
+
34
+ <tbody class="page">
35
+ <%= render @posts %>
36
+ </tbody>
37
+ </table>
38
+
39
+ <br>
40
+
41
+ <%= link_to 'New Post', new_post_path %>
42
+ <%= paginate @posts %>
43
+ ```
44
+
45
+ _post.html.erb
46
+ ```
47
+ <tr class="post">
48
+ <td><%= post.title %></td>
49
+ <td><%= post.author %></td>
50
+ <td><%= post.body %></td>
51
+ <td><%= link_to 'Show', post %></td>
52
+ <td><%= link_to 'Edit', edit_post_path(post) %></td>
53
+ <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
54
+ </tr>
55
+ ```
56
+
57
+ index.js.erb
58
+ ```
59
+ $("#posts").append("<tbody class='page'><%= escape_javascript(render(@posts)) %></tbody>");
60
+ ```
61
+
62
+ posts_controller.rb
63
+ ```
64
+ class PostsController < ApplicationController
65
+ before_action :set_post, only: [:show, :edit, :update, :destroy]
66
+
67
+ # GET /posts
68
+ # GET /posts.json
69
+ def index
70
+ @posts = Post.order(:created_at).page(params[:page])
71
+ end
72
+
73
+ # GET /posts/1
74
+ # GET /posts/1.json
75
+ def show
76
+ end
77
+
78
+ # GET /posts/new
79
+ def new
80
+ @post = Post.new
81
+ end
82
+
83
+ # GET /posts/1/edit
84
+ def edit
85
+ end
86
+
87
+ # POST /posts
88
+ # POST /posts.json
89
+ def create
90
+ @post = Post.new(post_params)
91
+
92
+ respond_to do |format|
93
+ if @post.save
94
+ format.html { redirect_to @post, notice: 'Post was successfully created.' }
95
+ format.json { render :show, status: :created, location: @post }
96
+ else
97
+ format.html { render :new }
98
+ format.json { render json: @post.errors, status: :unprocessable_entity }
99
+ end
100
+ end
101
+ end
102
+
103
+ # PATCH/PUT /posts/1
104
+ # PATCH/PUT /posts/1.json
105
+ def update
106
+ respond_to do |format|
107
+ if @post.update(post_params)
108
+ format.html { redirect_to @post, notice: 'Post was successfully updated.' }
109
+ format.json { render :show, status: :ok, location: @post }
110
+ else
111
+ format.html { render :edit }
112
+ format.json { render json: @post.errors, status: :unprocessable_entity }
113
+ end
114
+ end
115
+ end
116
+
117
+ # DELETE /posts/1
118
+ # DELETE /posts/1.json
119
+ def destroy
120
+ @post.destroy
121
+ respond_to do |format|
122
+ format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
123
+ format.json { head :no_content }
124
+ end
125
+ end
126
+
127
+ private
128
+ # Use callbacks to share common setup or constraints between actions.
129
+ def set_post
130
+ @post = Post.find(params[:id])
131
+ end
132
+
133
+ # Never trust parameters from the scary internet, only allow the white list through.
134
+ def post_params
135
+ params.require(:post).permit(:title, :author, :body)
136
+ end
137
+ end
138
+ ```
139
+
12
140
  ### 試したこと
13
141
 
14
142
  curlの取得先をhttps://unpkg.com/infinite-scroll@3.0.4/dist/infinite-scroll.pkgd.jsに修正して実施しました。