質問編集履歴
4
修正
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
3
修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
each使用して Ajax対応させるための記述方法
|
test
CHANGED
File without changes
|
2
補足
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
render使用してAjax対応させるための記述方法
|
1
|
+
render使用して Ajax対応させるための記述方法
|
test
CHANGED
File without changes
|
1
文章訂正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
render使用してAjax対応させるための記述方法
|
test
CHANGED
@@ -1,18 +1,30 @@
|
|
1
|
-
https://teratail.com/questions/124351
|
2
|
-
|
3
|
-
を質問させていただいた者です。問題内容が変わったので新たに質問させていただきたいと思います。
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
上の質問した後、ajaxには部分テンプレートが必須なんだということがわかりました。そこで部分テンプレートを用意したのですが未だ思うようにいかず重ねて質問させていただくことお許しください。
|
8
|
-
|
9
|
-
|
10
|
-
|
11
1
|
やりたいことはいわゆるいいね機能をAjaxでの導入です。Ajax未使用でのいいね機能は導入済みであります。
|
12
2
|
|
13
3
|
|
14
4
|
|
5
|
+
記述内にeachで表示していて、eachの中をrenderで部分テンプレートに移動、そこでAjax対応したい箇所を用意してあるのですが、そこのエラーがなかなか取れなくて困っています。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
一度each使用している部分ごとrenderにしてみたところ、Ajax対応が成功しました。が、他のページでもこのAjax対応を使いたいのでeachの中の一部分をrenderにしてAjax対応したいという次第です。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
現在Ajax非対応の記述ではうまく動作しており、Ajax対応させると、①remote: true→②コントローラアクション→③アクション.js.erb→④render先の部分テンプレートと移動しており、④の際にテンプレートエラーが出てきます。この時ですが、①と④は同じファイルを使用しております。
|
14
|
+
|
15
|
+
疑問ですが、①は通るのですが、④は通らないのがよくわかりません
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
また、意味はないと思いますが、④で別のファイル(①の remote:trueの記述だけ除いたもの)を用意してみたのですがダメでした。
|
20
|
+
|
21
|
+
|
22
|
+
|
15
|
-
|
23
|
+
恐らくコントローラーかjs.erbの記述がよくないと思うのですが正しい書方がわかりません。そこで、何かお気付きになられたことや解決方法など教えていただけると幸いです。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
以下ファイル載せます。
|
16
28
|
|
17
29
|
|
18
30
|
|
@@ -32,21 +44,11 @@
|
|
32
44
|
|
33
45
|
<td>
|
34
46
|
|
35
|
-
<div><%= product.title %></div>
|
36
|
-
|
37
|
-
<div><%= product.price %></div>
|
38
|
-
|
39
|
-
<div class="store_cart_in store_button">
|
40
|
-
|
41
|
-
<%= link_to image_tag('cart_in.png', :size => "83x30"), line_items_path(product_id: product), remote: true ,method: :post %>
|
42
|
-
|
43
|
-
</div>
|
44
|
-
|
45
47
|
<div id="favjs" class="store_button">
|
46
48
|
|
47
49
|
|
48
50
|
|
49
|
-
<%= render
|
51
|
+
<%= render 'favorites/favorite',product: product %>
|
50
52
|
|
51
53
|
|
52
54
|
|
@@ -90,9 +92,7 @@
|
|
90
92
|
|
91
93
|
```
|
92
94
|
|
93
|
-
$("#notice").hide();
|
94
|
-
|
95
|
-
$(
|
95
|
+
$('#favjs').html("<%=j render 'favorites/favorite' %>");
|
96
96
|
|
97
97
|
|
98
98
|
|
@@ -102,27 +102,27 @@
|
|
102
102
|
|
103
103
|
```
|
104
104
|
|
105
|
-
ActionView::Template::Error (undefined local variable or method `product' for #<#<Class:0xb
|
105
|
+
ActionView::Template::Error (undefined local variable or method `product' for #<#<Class:0x0bacf034>:0x0a69c238>):
|
106
|
-
|
106
|
+
|
107
|
-
1:
|
107
|
+
1: <%# @products.each do |product| %>
|
108
108
|
|
109
109
|
2: <% unless session[:user_id].nil? %>
|
110
110
|
|
111
|
-
3: <% if product.favorited_by? current_user %>
|
111
|
+
3: <% if product.favorited_by? current_user %>
|
112
|
-
|
112
|
+
|
113
|
-
4: <%= link_to image_tag('no_fav.png', :size => "83x30"), product_favorites_path(product),
|
113
|
+
4: <%= link_to image_tag('no_fav.png', :size => "83x30"), product_favorites_path(product) ,remote: true,method: :delete %>
|
114
|
-
|
114
|
+
|
115
|
-
5: <% else %>
|
115
|
+
5: <% else %>
|
116
|
-
|
116
|
+
|
117
|
-
6: <%= link_to image_tag('add_fav.png', :size => "83x30"), product_favorites_path(product),
|
117
|
+
6: <%= link_to image_tag('add_fav.png', :size => "83x30"), product_favorites_path(product),remote: true,method: :post %>
|
118
118
|
|
119
119
|
|
120
120
|
|
121
|
-
app/views/favorites/_favorite.html.erb:3:in `_app_views_favorites__favorite_html_erb__
|
121
|
+
app/views/favorites/_favorite.html.erb:3:in `_app_views_favorites__favorite_html_erb__914006175_87697530'
|
122
|
-
|
123
|
-
|
122
|
+
|
124
|
-
|
125
|
-
app/views/
|
123
|
+
app/views/favorites/create.js.erb:1:in `_app_views_favorites_create_js_erb___327707278_87357230'
|
124
|
+
|
125
|
+
|
126
126
|
|
127
127
|
```
|
128
128
|
|
@@ -140,6 +140,8 @@
|
|
140
140
|
|
141
141
|
def create
|
142
142
|
|
143
|
+
@products = Product.paginate(:page => params[:page], :per_page => 12)
|
144
|
+
|
143
145
|
@product = Product.find(params[:product_id])
|
144
146
|
|
145
147
|
@favorite = current_user.favorites.build(product: @product)
|
@@ -186,39 +188,45 @@
|
|
186
188
|
|
187
189
|
end
|
188
190
|
|
189
|
-
|
190
|
-
|
191
191
|
end
|
192
192
|
|
193
|
-
|
194
|
-
|
195
|
-
```
|
193
|
+
```
|
196
|
-
|
194
|
+
|
197
|
-
|
195
|
+
store_controller.rb
|
198
|
-
|
199
|
-
|
200
|
-
|
196
|
+
|
201
|
-
```
|
197
|
+
```
|
198
|
+
|
202
|
-
|
199
|
+
class StoreController < ApplicationController
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
def index
|
204
|
+
|
205
|
+
@cart = current_cart
|
206
|
+
|
207
|
+
@categories = Category.all
|
208
|
+
|
203
|
-
|
209
|
+
@user = current_user
|
210
|
+
|
204
|
-
|
211
|
+
@products = Product.paginate(:page => params[:page], :per_page => 12)
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
if params[:title].present?
|
216
|
+
|
205
|
-
|
217
|
+
@products = @products.get_by_name params[:title]
|
218
|
+
|
206
|
-
|
219
|
+
end
|
207
|
-
|
208
220
|
|
209
221
|
end
|
210
222
|
|
223
|
+
end
|
224
|
+
|
225
|
+
|
226
|
+
|
211
|
-
```
|
227
|
+
```
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
228
|
+
|
216
|
-
|
217
|
-
|
229
|
+
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
なかなかうまくいかず正解に近づいて行っているのか遠ざかっているのか不安なため重複して質問してしまいました。
|
222
230
|
|
223
231
|
|
224
232
|
|