each内部分テンプレートへのデータの渡し方
受付中
回答 0
投稿
- 評価
- クリップ 0
- VIEW 609
Ajaxの対応したいのですがうまくいかず、そもそもデータの渡し方が悪いとだと思い質問させていただきます。
each部分を全て部分テンプレートに含ませた場合はajax対応がうまくできたのですが、不要な部分が多いので、必要な部分だけを部分テンプレートにしてajax対応したいのですが、全くうまくいかず困っております。
ひとまずデータ添付させていただきます
store/index.html.erb
<div>
<% @products.each do |product| %>
<table>
<tbody>
<tr>
<td>
<div id="favjs" class="store_button">
<%= render 'favorites/favorite',product: product %>
</div>
</td>
</tr>
</tbody>
</table>
<% end %>
</div>
このようにeachの中にrenderがありまして、そこにremoto:trueで置き換えたい部分があるのですが、記述の仕方がわかりません。調べてみたもののどれもいい結果が得られないのでご教授いただけたら幸いです。
以下部分テンプレートです
favorites/_favorite.html.erb
<% unless session[:user_id].nil? %>
<% if product.favorited_by? current_user %>
<%= link_to image_tag('no_fav.png', :size => "83x30"), product_favorites_path(product) ,remote: true, method: :delete %>
<% else %>
<%= link_to image_tag('add_fav.png', :size => "83x30"), product_favorites_path(product),remote: true, method: :post %>
<% end %>
<% end %>
store_controller.rb
class StoreController < ApplicationController
def index
@cart = current_cart
@categories = Category.all
@user = current_user
@products = Product.paginate(:page => params[:page], :per_page => 12)
end
end
favorites_controller.rb
class FavoritesController < ApplicationController
before_action :authenticate
def create
@product = Product.find(params[:product_id])
@products = Product.paginate(:page => params[:page], :per_page => 12)
@favorite = current_user.favorites.build(product: @product)
respond_to do |format|
if @favorite.save
format.html{redirect_to store_path, notice: "お気に入りに登録しました"}
format.js
format.json{head :ok}
else
format.html{redirect_to store_path, notice:"この商品はお気に入りに登録できません"}
end
end
end
def destroy
@product = Product.find(params[:product_id])
@products = Product.paginate(:page => params[:page], :per_page => 12)
@favorite = current_user.favorites.find_by!(product_id: params[:product_id])
@favorite.destroy
respond_to do |format|
format.html{redirect_to store_path, notice:"お気に入りを解除しました"}
format.js
format.json{head :ok}
end
end
end
create.js.erbとdestroy.js.erb
$('#favjs').html("<%=j render 'store/index', product: @product %>");
store/_index.html.erbは_favorite.html.erbからremote: trueを削除したファイルになります
データは以上です。
エラーが出ているところは
Rendered favorites/_index.html.erb (164.1ms)
Rendered favorites/destroy.js.erb (198.7ms)
Completed 500 Internal Server Error in 362ms (ActiveRecord: 20.5ms)
ActionView::Template::Error (undefined local variable or method `product' for #<#<Class:0x0d5d6d3c>:0x0df8fa2c>):
1: <%# render 'favorites/favorite' %>
2:
3: <% unless session[:user_id].nil? %>
4: <% if product.favorited_by? current_user %>
5: <%= link_to image_tag('no_fav.png', :size => "83x30"), product_favorites_path(product) ,method: :delete %>
6: <% else %>
7: <%= link_to image_tag('add_fav.png', :size => "83x30"), product_favorites_path(product),method: :post %>
app/views/favorites/_index.html.erb:4:in `_app_views_favorites__index_html_erb__134264475_116445340'
app/views/favorites/destroy.js.erb:1:in `_app_views_favorites_destroy_js_erb___472040285_115900460'
こちらはdestroyの時になります。createの時なんですが、先頭のproductだけはcreateだけajax対応されるのですが先頭のもの以外のcreateボタン押すと、先頭のproductが反応します。createされてなかったらcreateされて、create済みだったらcreateアクションのelseになります。
なので部分テンプレートへの戻り値の記述がおかしいのだと思うのですが、そこが全くわかりません。
コントローラーの.save時に@〜に何か入れるのか、アクション.js.erbのところにデータを代入するのかと思っているのですがよくわかりませんでした。
何かお気付きの方いらっしゃいましたらアドバイスのほう宜しくお願いします。
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
まだ回答がついていません
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.23%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる