回答編集履歴
5
参考情報であることを強調
answer
CHANGED
@@ -2,13 +2,17 @@
|
|
2
2
|
であれば、いろいろとやり方あると思いますが、imageテーブルが仮にあるとして
|
3
3
|
|
4
4
|
```rb
|
5
|
+
# これは参考情報です
|
5
6
|
=> #<Image:0x00007fe2a605b191
|
6
7
|
"id": 201,
|
7
8
|
"filename": "gazou-100maime",
|
8
9
|
"created_at": "2018-01-20 00:00:20",
|
9
10
|
"updated_at": ...
|
10
11
|
|
12
|
+
|
11
|
-
|
13
|
+
# 下記は適宜質問者様の都合の良いModelに読み替えてください
|
14
|
+
# 今回の件で言えば、Product Model
|
15
|
+
class Image
|
12
16
|
def is_new
|
13
17
|
(3.days.ago..Time.current).cover?(Time.parse(self.created_at))
|
14
18
|
end
|
4
image_tagの使い方を追記
answer
CHANGED
@@ -35,8 +35,9 @@
|
|
35
35
|
<% @products.each do |product| %>
|
36
36
|
<div class="col-xs-12">
|
37
37
|
<% if product.is_new %>
|
38
|
-
<%=
|
38
|
+
<%= image_tag 'NEW' %>
|
39
39
|
<% end %>
|
40
|
+
<%= attachment_image_tag product, :image , class: "product_image" %>
|
40
41
|
</div>
|
41
42
|
<% end %>
|
42
43
|
```
|
3
if文の間違い修正
answer
CHANGED
@@ -34,7 +34,7 @@
|
|
34
34
|
```rb
|
35
35
|
<% @products.each do |product| %>
|
36
36
|
<div class="col-xs-12">
|
37
|
-
<%
|
37
|
+
<% if product.is_new %>
|
38
38
|
<%= attachment_image_tag product, :image , class: "product_image" %>
|
39
39
|
<% end %>
|
40
40
|
</div>
|
2
コメントの追加
answer
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
"created_at": "2018-01-20 00:00:20",
|
9
9
|
"updated_at": ...
|
10
10
|
|
11
|
-
class Image
|
11
|
+
class Image # ここは適宜質問者様の都合の良いモデルに読み替えてください
|
12
12
|
def is_new
|
13
13
|
(3.days.ago..Time.current).cover?(Time.parse(self.created_at))
|
14
14
|
end
|
1
どう実装したら良いのかに対しての追記
answer
CHANGED
@@ -15,4 +15,28 @@
|
|
15
15
|
end
|
16
16
|
```
|
17
17
|
|
18
|
-
このis_newをあとはview側で呼び出してあげるだけです
|
18
|
+
このis_newをあとはview側で呼び出してあげるだけです
|
19
|
+
|
20
|
+
**追記**
|
21
|
+
|
22
|
+
修正前:
|
23
|
+
|
24
|
+
```rb
|
25
|
+
<% @products.each do |product| %>
|
26
|
+
<div class="col-xs-12">
|
27
|
+
<%= attachment_image_tag product, :image , class: "product_image" %>
|
28
|
+
</div>
|
29
|
+
<% end %>
|
30
|
+
```
|
31
|
+
|
32
|
+
修正後:
|
33
|
+
|
34
|
+
```rb
|
35
|
+
<% @products.each do |product| %>
|
36
|
+
<div class="col-xs-12">
|
37
|
+
<%= if product.is_new %>
|
38
|
+
<%= attachment_image_tag product, :image , class: "product_image" %>
|
39
|
+
<% end %>
|
40
|
+
</div>
|
41
|
+
<% end %>
|
42
|
+
```
|