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のところにデータを代入するのかと思っているのですがよくわかりませんでした。
何かお気付きの方いらっしゃいましたらアドバイスのほう宜しくお願いします。
あなたの回答
tips
プレビュー