Ajax-enabled description

Asked 2 years ago, Updated 2 years ago, 144 views

I am writing an ec site, but Ajax support is not going as well as I would like, so I would like to ask you a question.

I would like to use Ajax to switch between favorites and favorites, but I am currently creating a remote:true and js.html file, a partial template, but when I add or remove favorites, the screen does not change, and when I refresh the screen, I press the button.

As for how to display it, the layout page has 3 columns longitudinally, and both sides are described in layout, and the center is changed to various pages with yield.

There are two types of remote:true in the _store.html.erb displayed on the render, one to add to the cart and the other to your liking.The cart is always displayed in the right column on the layout page, and Ajax is doing well.
Regarding the error, if remote:true is removed, it will work normally, but it will transition to the route page every time, so I would like to accommodate Ajax.

I don't know how to deal with it, so if you notice anything, please give me advice or point it out.

 ActionView:: Template:: Error (undefined local variable or method `product' for #<#<Class:0x0backf034>:0x0d5c4600>):
1:<%#@products.eachdo|product|%>
2:<%unless session[:user_id].nil?%>
3:<%if product.favored_by?current_user%>
4:<%=link_to image_tag('no_fav.png',:size=>"83x30"), product_favorites_path(product), remote:true, method::delete%>
5:<%else%>
6:<%=link_to image_tag('add_fav.png',:size=>"83x30"), product_favorites_path(product), remote:true, method:::post%>

app/views/favorites/_favorite.html.erb:3:in`_app_views_favorites__favorites_html_erb_914006175_107084170'
app/views/favorites/create.js.erb:1:in`_app_views_favorites_create_js_erb__327707278_112076230'

#view/store/index.html.erb

<div>
<%@products.eachdo|product|%>
<table>    
<tbody>
    <tr>
        <td>
            <divid="favjs" class="store_button">

                <%=render 'favorites/favorite', product:product%>

            </div>                
        </td>
    </tr>
</tbody>
</table>
<%end%>
</div>

# view /favorites/_favorite.html.erb

<%unless session[:user_id].nil?%>
                <%if product.favored_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%>
            

#favorites_controller.rb

class FavoritesController<ApplicationController
        before_action —Authenticate

def create
    @product=Product.find (params[:product_id])
    @favorite=current_user.favorites.build(product:@product)
    @products=Product.paginate(:page=>params[:page],:per_page=>12

    response_to do | format |
        [email protected]
            format.html {redirect_to store_path, notice: "Favorite registered"}
            format.js
            format.json {head:ok}           
        else
            format.html {redirect_to store_path, notice: "This item cannot be registered as a favorite"}
        end
    end
end

def destroy

    @products=Product.paginate(:page=>params[:page],:per_page=>12
    @favorite=current_user.favorites.find_by!(product_id:params[:product_id])
    @favorite.destroy

    response_to do | format |
        format.html {redirect_to store_path, notice: "Unleashed Favorites"}
        format.js
        format.json {head:ok}
    end 
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)

    if params [:title].present?
        @[email protected]_by_name params[:title]
    end
    
end
end

#favorites/create.js.erb

$('#favjs').html("<%=jrender'favorites/favorite'%>");

#favorites/destroy.js.erb

$('#favjs').html("<%=jrender'favorites/favorite'%>");

Additional information

For both js.erb files, add product:@product as $('#favjs') .html("<%=jrender'favorites/index', product:@product%>"; and
I made the render destination a new file.

favoritees/_index.html.erb (only _favorite remote:true is turned off)

Then, only the first product output on each became Ajax compatible.I can't press the create button on another product because the create of the first product works. The destroy action is not Ajax-enabled.
I hope it will be helpful.Please continue to ask this question m(._.)m

javascript ruby-on-rails ruby ajax

2022-09-30 21:30

1 Answers

There may be other problems, but regarding the error,

<%#@products.eachdo|product|%>

I have already commented out in , so

<%@products.eachdo|product|%>

Please remove the comment.


2022-09-30 21:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.