How to remove order item with Ajax from Rails app










0















I've been trying to remove an order item from my cart view with Ajax... For the moment, if I press Delete button, the order item is removed from db, but not from the view and my code below does not work(I have very little understanding of ajax/jquery, so maybe an explantion a little more detailed would help):



controller.rb:



def destroy
@order_line.destroy
respond_to do |format|
format.html redirect_to :action => "show", :id => current_order_id
format.js
end
if last_order_item?(@order)
@order.destroy
redirect_to books_url
end
end


view.html.erb:



<% @order.order_lines.order(:book_id).each do |line| %>
<div class="order_line_row" id = "orderline-<%= line.id%>">
<div class="book_details">
<% if File.file?("#Rails.root/app/assets/images/#line.book.image") %>
<%= image_tag line.book.image, class: "card-img-top", alt: "test image" %>
<% else %>
<%= image_tag "images.jpeg", class: "card-img-top", alt: "test image" %>
<% end %>
<h5 class="product-name"><%= line.book.title %></h5>
<%= link_to 'Delete book', order_line_path(line.id), method: :delete, remote: true, class: "btn btn-primary" %>
</div>

<div class="quantity">
<%= form_for(line) do |f| %>
<%= f.label :qty %></br>
<%= f.number_field :qty, in: 1...line.book.stock %>
<%= f.submit "Update", class: "btn btn-primary" %>
<% end %>
</div>

<div class="price">Price</br><%= line.subtotal_price %></div>

</div>
<hr>
<% end %>


destroy.js.rb:



$(document).ready(function()
$('#orderline-<%= @line.id %>').remove();
)


Thanks for your time...










share|improve this question






















  • Are you using turbolinks? If so, you might try $(document).on "turbolinks:load" instead of $(document).ready as discussed in the docs.

    – jvillian
    Nov 15 '18 at 14:45











  • @jvillian, I've tried that but it's not working.

    – himn
    Nov 15 '18 at 16:10











  • I also noticed you're trying to do string interpolation here: '#orderline-<%= @line.id %>' using single quotes. But it only works with double quotes. Also try: "#orderline-<%= @line.id %>".

    – jvillian
    Nov 15 '18 at 16:18











  • String interpolation looks like `"Ordered by # @order.name"'. @himn is just injecting Ruby code that will create part of the Javascript. The Javascript itself will run fine with single quotes or double quotes.

    – Fordini
    Nov 16 '18 at 17:22















0















I've been trying to remove an order item from my cart view with Ajax... For the moment, if I press Delete button, the order item is removed from db, but not from the view and my code below does not work(I have very little understanding of ajax/jquery, so maybe an explantion a little more detailed would help):



controller.rb:



def destroy
@order_line.destroy
respond_to do |format|
format.html redirect_to :action => "show", :id => current_order_id
format.js
end
if last_order_item?(@order)
@order.destroy
redirect_to books_url
end
end


view.html.erb:



<% @order.order_lines.order(:book_id).each do |line| %>
<div class="order_line_row" id = "orderline-<%= line.id%>">
<div class="book_details">
<% if File.file?("#Rails.root/app/assets/images/#line.book.image") %>
<%= image_tag line.book.image, class: "card-img-top", alt: "test image" %>
<% else %>
<%= image_tag "images.jpeg", class: "card-img-top", alt: "test image" %>
<% end %>
<h5 class="product-name"><%= line.book.title %></h5>
<%= link_to 'Delete book', order_line_path(line.id), method: :delete, remote: true, class: "btn btn-primary" %>
</div>

<div class="quantity">
<%= form_for(line) do |f| %>
<%= f.label :qty %></br>
<%= f.number_field :qty, in: 1...line.book.stock %>
<%= f.submit "Update", class: "btn btn-primary" %>
<% end %>
</div>

<div class="price">Price</br><%= line.subtotal_price %></div>

</div>
<hr>
<% end %>


destroy.js.rb:



$(document).ready(function()
$('#orderline-<%= @line.id %>').remove();
)


Thanks for your time...










share|improve this question






















  • Are you using turbolinks? If so, you might try $(document).on "turbolinks:load" instead of $(document).ready as discussed in the docs.

    – jvillian
    Nov 15 '18 at 14:45











  • @jvillian, I've tried that but it's not working.

    – himn
    Nov 15 '18 at 16:10











  • I also noticed you're trying to do string interpolation here: '#orderline-<%= @line.id %>' using single quotes. But it only works with double quotes. Also try: "#orderline-<%= @line.id %>".

    – jvillian
    Nov 15 '18 at 16:18











  • String interpolation looks like `"Ordered by # @order.name"'. @himn is just injecting Ruby code that will create part of the Javascript. The Javascript itself will run fine with single quotes or double quotes.

    – Fordini
    Nov 16 '18 at 17:22













0












0








0








I've been trying to remove an order item from my cart view with Ajax... For the moment, if I press Delete button, the order item is removed from db, but not from the view and my code below does not work(I have very little understanding of ajax/jquery, so maybe an explantion a little more detailed would help):



controller.rb:



def destroy
@order_line.destroy
respond_to do |format|
format.html redirect_to :action => "show", :id => current_order_id
format.js
end
if last_order_item?(@order)
@order.destroy
redirect_to books_url
end
end


view.html.erb:



<% @order.order_lines.order(:book_id).each do |line| %>
<div class="order_line_row" id = "orderline-<%= line.id%>">
<div class="book_details">
<% if File.file?("#Rails.root/app/assets/images/#line.book.image") %>
<%= image_tag line.book.image, class: "card-img-top", alt: "test image" %>
<% else %>
<%= image_tag "images.jpeg", class: "card-img-top", alt: "test image" %>
<% end %>
<h5 class="product-name"><%= line.book.title %></h5>
<%= link_to 'Delete book', order_line_path(line.id), method: :delete, remote: true, class: "btn btn-primary" %>
</div>

<div class="quantity">
<%= form_for(line) do |f| %>
<%= f.label :qty %></br>
<%= f.number_field :qty, in: 1...line.book.stock %>
<%= f.submit "Update", class: "btn btn-primary" %>
<% end %>
</div>

<div class="price">Price</br><%= line.subtotal_price %></div>

</div>
<hr>
<% end %>


destroy.js.rb:



$(document).ready(function()
$('#orderline-<%= @line.id %>').remove();
)


Thanks for your time...










share|improve this question














I've been trying to remove an order item from my cart view with Ajax... For the moment, if I press Delete button, the order item is removed from db, but not from the view and my code below does not work(I have very little understanding of ajax/jquery, so maybe an explantion a little more detailed would help):



controller.rb:



def destroy
@order_line.destroy
respond_to do |format|
format.html redirect_to :action => "show", :id => current_order_id
format.js
end
if last_order_item?(@order)
@order.destroy
redirect_to books_url
end
end


view.html.erb:



<% @order.order_lines.order(:book_id).each do |line| %>
<div class="order_line_row" id = "orderline-<%= line.id%>">
<div class="book_details">
<% if File.file?("#Rails.root/app/assets/images/#line.book.image") %>
<%= image_tag line.book.image, class: "card-img-top", alt: "test image" %>
<% else %>
<%= image_tag "images.jpeg", class: "card-img-top", alt: "test image" %>
<% end %>
<h5 class="product-name"><%= line.book.title %></h5>
<%= link_to 'Delete book', order_line_path(line.id), method: :delete, remote: true, class: "btn btn-primary" %>
</div>

<div class="quantity">
<%= form_for(line) do |f| %>
<%= f.label :qty %></br>
<%= f.number_field :qty, in: 1...line.book.stock %>
<%= f.submit "Update", class: "btn btn-primary" %>
<% end %>
</div>

<div class="price">Price</br><%= line.subtotal_price %></div>

</div>
<hr>
<% end %>


destroy.js.rb:



$(document).ready(function()
$('#orderline-<%= @line.id %>').remove();
)


Thanks for your time...







ruby-on-rails ajax






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 14:42









himnhimn

214




214












  • Are you using turbolinks? If so, you might try $(document).on "turbolinks:load" instead of $(document).ready as discussed in the docs.

    – jvillian
    Nov 15 '18 at 14:45











  • @jvillian, I've tried that but it's not working.

    – himn
    Nov 15 '18 at 16:10











  • I also noticed you're trying to do string interpolation here: '#orderline-<%= @line.id %>' using single quotes. But it only works with double quotes. Also try: "#orderline-<%= @line.id %>".

    – jvillian
    Nov 15 '18 at 16:18











  • String interpolation looks like `"Ordered by # @order.name"'. @himn is just injecting Ruby code that will create part of the Javascript. The Javascript itself will run fine with single quotes or double quotes.

    – Fordini
    Nov 16 '18 at 17:22

















  • Are you using turbolinks? If so, you might try $(document).on "turbolinks:load" instead of $(document).ready as discussed in the docs.

    – jvillian
    Nov 15 '18 at 14:45











  • @jvillian, I've tried that but it's not working.

    – himn
    Nov 15 '18 at 16:10











  • I also noticed you're trying to do string interpolation here: '#orderline-<%= @line.id %>' using single quotes. But it only works with double quotes. Also try: "#orderline-<%= @line.id %>".

    – jvillian
    Nov 15 '18 at 16:18











  • String interpolation looks like `"Ordered by # @order.name"'. @himn is just injecting Ruby code that will create part of the Javascript. The Javascript itself will run fine with single quotes or double quotes.

    – Fordini
    Nov 16 '18 at 17:22
















Are you using turbolinks? If so, you might try $(document).on "turbolinks:load" instead of $(document).ready as discussed in the docs.

– jvillian
Nov 15 '18 at 14:45





Are you using turbolinks? If so, you might try $(document).on "turbolinks:load" instead of $(document).ready as discussed in the docs.

– jvillian
Nov 15 '18 at 14:45













@jvillian, I've tried that but it's not working.

– himn
Nov 15 '18 at 16:10





@jvillian, I've tried that but it's not working.

– himn
Nov 15 '18 at 16:10













I also noticed you're trying to do string interpolation here: '#orderline-<%= @line.id %>' using single quotes. But it only works with double quotes. Also try: "#orderline-<%= @line.id %>".

– jvillian
Nov 15 '18 at 16:18





I also noticed you're trying to do string interpolation here: '#orderline-<%= @line.id %>' using single quotes. But it only works with double quotes. Also try: "#orderline-<%= @line.id %>".

– jvillian
Nov 15 '18 at 16:18













String interpolation looks like `"Ordered by # @order.name"'. @himn is just injecting Ruby code that will create part of the Javascript. The Javascript itself will run fine with single quotes or double quotes.

– Fordini
Nov 16 '18 at 17:22





String interpolation looks like `"Ordered by # @order.name"'. @himn is just injecting Ruby code that will create part of the Javascript. The Javascript itself will run fine with single quotes or double quotes.

– Fordini
Nov 16 '18 at 17:22












1 Answer
1






active

oldest

votes


















0














Your destroy.js.erb should just be:



 $('#orderline-<%= @line.id %>').remove();


The .ready() function only runs after the DOM is loaded. You want to change an element that has already been loaded.



There are couple of problems with the destroy controller method, too.



 def destroy

# Add next line to create the @order_line object
@order_line = OrderLine.find(params[:id])

@order_line.destroy
respond_to do |format|
format.html redirect_to :action => "show", :id => current_order_id
format.js
end

# None of this code will execute because the response
# has already been rendered. This needs to be before respond_to
if last_order_item?(@order)
@order.destroy
redirect_to books_url
end
end





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321912%2fhow-to-remove-order-item-with-ajax-from-rails-app%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Your destroy.js.erb should just be:



     $('#orderline-<%= @line.id %>').remove();


    The .ready() function only runs after the DOM is loaded. You want to change an element that has already been loaded.



    There are couple of problems with the destroy controller method, too.



     def destroy

    # Add next line to create the @order_line object
    @order_line = OrderLine.find(params[:id])

    @order_line.destroy
    respond_to do |format|
    format.html redirect_to :action => "show", :id => current_order_id
    format.js
    end

    # None of this code will execute because the response
    # has already been rendered. This needs to be before respond_to
    if last_order_item?(@order)
    @order.destroy
    redirect_to books_url
    end
    end





    share|improve this answer



























      0














      Your destroy.js.erb should just be:



       $('#orderline-<%= @line.id %>').remove();


      The .ready() function only runs after the DOM is loaded. You want to change an element that has already been loaded.



      There are couple of problems with the destroy controller method, too.



       def destroy

      # Add next line to create the @order_line object
      @order_line = OrderLine.find(params[:id])

      @order_line.destroy
      respond_to do |format|
      format.html redirect_to :action => "show", :id => current_order_id
      format.js
      end

      # None of this code will execute because the response
      # has already been rendered. This needs to be before respond_to
      if last_order_item?(@order)
      @order.destroy
      redirect_to books_url
      end
      end





      share|improve this answer

























        0












        0








        0







        Your destroy.js.erb should just be:



         $('#orderline-<%= @line.id %>').remove();


        The .ready() function only runs after the DOM is loaded. You want to change an element that has already been loaded.



        There are couple of problems with the destroy controller method, too.



         def destroy

        # Add next line to create the @order_line object
        @order_line = OrderLine.find(params[:id])

        @order_line.destroy
        respond_to do |format|
        format.html redirect_to :action => "show", :id => current_order_id
        format.js
        end

        # None of this code will execute because the response
        # has already been rendered. This needs to be before respond_to
        if last_order_item?(@order)
        @order.destroy
        redirect_to books_url
        end
        end





        share|improve this answer













        Your destroy.js.erb should just be:



         $('#orderline-<%= @line.id %>').remove();


        The .ready() function only runs after the DOM is loaded. You want to change an element that has already been loaded.



        There are couple of problems with the destroy controller method, too.



         def destroy

        # Add next line to create the @order_line object
        @order_line = OrderLine.find(params[:id])

        @order_line.destroy
        respond_to do |format|
        format.html redirect_to :action => "show", :id => current_order_id
        format.js
        end

        # None of this code will execute because the response
        # has already been rendered. This needs to be before respond_to
        if last_order_item?(@order)
        @order.destroy
        redirect_to books_url
        end
        end






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 17:15









        FordiniFordini

        813




        813





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321912%2fhow-to-remove-order-item-with-ajax-from-rails-app%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto