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

質問編集履歴

3

情報の修正を行いました。

2021/06/11 10:28

投稿

n-s0120
n-s0120

スコア1

title CHANGED
File without changes
body CHANGED
@@ -13,6 +13,9 @@
13
13
  画像2
14
14
  ![イメージ説明](cfda17f9af4ace409e695549279387a5.png)
15
15
 
16
+ 画像3(修正後)
17
+ ![イメージ説明](e19aa32ea3d23622e7fb9bacdd0ed109.png)
18
+
16
19
  ### 発生している問題・エラーメッセージ
17
20
  5段階評価で表示したいが、一瞬だけ、星が10個表示されてしまう。
18
21
  ```
@@ -64,38 +67,10 @@
64
67
  </thead>
65
68
  <tbody>
66
69
  <% books.each do |book| %>
67
- <tr>
68
- <td>
69
- <%= link_to user_path(book.user) do %>
70
- <%= attachment_image_tag(book.user, :profile_image, :fill, 50, 50, fallback: "no-image-icon.jpg") %>
71
- <% end %>
72
- </td>
73
- <td><%= link_to book.title, book_path(book), class: "book_#{book.id}" %></td>
74
- <td><%= book.body %></td>
75
-
76
- <td id="favorite-btn-<%= book.id %>">
77
- <%= render partial: "favorites/favorite-btn",locals: {book: book } %>
70
+ <%= render 'books/book', book: book %>
78
- </td>
79
-
80
- <td id="comments">
81
- コメント数:<%= book.book_comments.count %>
82
- </td>
83
-
84
- <td class="book-evaluation" data-score="<%= book.evaluation %>"></td>
85
- </tr>
86
71
  <% end %>
87
72
  </tbody>
88
73
  </table>
89
-
90
- <script>
91
- $('.book-evaluation').raty({
92
- readOnly: true,
93
- score: function() {
94
- return $(this).attr('data-score');
95
- },
96
- path: '/assets/'
97
- });
98
- </script>
99
74
  ```
100
75
 
101
76
  ```Ruby
@@ -202,6 +177,38 @@
202
177
 
203
178
  ```
204
179
 
180
+ ```Ruby
181
+
182
+ 【books/_book.html.erb】
183
+ <tr>
184
+ <td>
185
+ <%= link_to user_path(book.user) do %>
186
+ <%= attachment_image_tag(book.user, :profile_image, :fill, 50, 50, fallback: "no-image-icon.jpg") %>
187
+ <% end %>
188
+ </td>
189
+ <td><%= link_to book.title, book_path(book), class: "book_#{book.id}" %></td>
190
+ <td><%= book.body %></td>
191
+
192
+ <td id="favorite-btn-<%= book.id %>">
193
+ <%= render partial: "favorites/favorite-btn",locals: {book: book } %>
194
+ </td>
195
+
196
+ <td id="comments">
197
+ コメント数:<%= book.book_comments.count %>
198
+ </td>
199
+
200
+ <td class="book-evaluation" data-score="<%= book.evaluation %>"></td>
201
+ </tr>
202
+
203
+ <script>
204
+ $('.book-evaluation').raty({
205
+ readOnly: true,
206
+ path: '/assets/'
207
+ });
208
+ </script>
209
+
210
+ ```
211
+
205
212
  ### 試したこと
206
213
 
207
214
  classやidの変更などを行いましたが、上手く解決できませんでした。

2

情報の追加を行いました。

2021/06/11 10:28

投稿

n-s0120
n-s0120

スコア1

title CHANGED
File without changes
body CHANGED
@@ -98,6 +98,110 @@
98
98
  </script>
99
99
  ```
100
100
 
101
+ ```Ruby
102
+
103
+ 【books_controller.erb】
104
+
105
+ class BooksController < ApplicationController
106
+
107
+ def show
108
+ @book = Book.find(params[:id])
109
+ @book_new = Book.new
110
+ @user = @book.user
111
+ @book_comment = BookComment.new
112
+ end
113
+
114
+ def index
115
+ @books = Book.all
116
+ @book = Book.new
117
+ end
118
+
119
+ def create
120
+ @book = Book.new(book_params)
121
+ @book.user_id = current_user.id
122
+ if @book.save
123
+ redirect_to book_path(@book), notice: "You have created book successfully."
124
+ else
125
+ @books = Book.all
126
+ render 'index'
127
+ end
128
+ end
129
+
130
+ def edit
131
+ @book = Book.find(params[:id])
132
+ @user = @book.user
133
+ if @user == current_user
134
+ render :edit
135
+ else
136
+ redirect_to books_path
137
+ end
138
+ end
139
+
140
+ def update
141
+ @book = Book.find(params[:id])
142
+ if @book.update(book_params)
143
+ redirect_to book_path(@book), notice: "You have updated book successfully."
144
+ else
145
+ render "edit"
146
+ end
147
+ end
148
+
149
+ def destroy
150
+ @book = Book.find(params[:id])
151
+ @book.delete
152
+ redirect_to books_path
153
+ end
154
+
155
+ def search
156
+ @range = params[:range]
157
+ search = params[:search]
158
+ keyword = params[:keyword]
159
+ @books = User.search(search, keyword)
160
+ end
161
+
162
+ private
163
+
164
+ def book_params
165
+ params.require(:book).permit(:title, :body, :evaluation)
166
+ end
167
+
168
+ end
169
+
170
+ ```
171
+
172
+ ```Ruby
173
+ 【book.rb】
174
+ class Book < ApplicationRecord
175
+ belongs_to :user
176
+ has_many :book_comments, dependent: :destroy
177
+ has_many :favorites, dependent: :destroy
178
+
179
+ def favorited_by?(user)
180
+ favorites.where(user_id: user.id).exists?
181
+ end
182
+
183
+ validates :title, presence: true
184
+ validates :body, presence: true, length: {maximum: 200}
185
+ validates :evaluation, numericality: {
186
+ less_than_or_equal_to: 5,
187
+ greater_than_or_equal_to: 1
188
+ }
189
+
190
+ def self.search(searches, keywords)
191
+ if searches == "perfect_match"
192
+ @book = Book.where("title LIKE? OR body LIKE?", "#{keywords}", "#{keywords}")
193
+ elsif searches == "forward_match"
194
+ @book = Book.where("title LIKE? OR body LIKE?", "#{keywords}%", "#{keywords}%")
195
+ elsif searches == "backward_match"
196
+ @book = Book.where("title LIKE? OR body LIKE?", "%#{keywords}", "%#{keywords}")
197
+ else
198
+ @book = Book.where("title LIKE? OR body LIKE?", "%#{keywords}%", "%#{keywords}%")
199
+ end
200
+ end
201
+ end
202
+
203
+ ```
204
+
101
205
  ### 試したこと
102
206
 
103
207
  classやidの変更などを行いましたが、上手く解決できませんでした。

1

javascriptの問題である可能性があるため、javascriptを追加しました。

2021/06/09 14:33

投稿

n-s0120
n-s0120

スコア1

title CHANGED
File without changes
body CHANGED
File without changes