2

Getting back into programming but I'm having trouble with this basic thing. So I've scraped products from a web site then inserted them into a DB. Then I list those products on my web site. Now I'm trying to add a delete button next to each of those product that are listed on my web site. I've tried using the solutions found on stackoverflow but I can't seem to get any of them to work. I know this is a basic question, but I appreciate the help.

Controller

class IbottaController < ApplicationController

def save
    require 'watir'
    require 'phantomjs'

    @browser = Watir::Browser.new:phantomjs
    @browser.goto "https://ibotta.com/rebates"
    @button = @browser.button(class: "see-more-label")

    Ibotta.delete_all
    # if x = 24 then I get 492 products 
    # if x = 23 then I get 472 products
    x = 24
    y = 0
    while y < x 
    @button.click
    y+=1
    end

    @products = @browser.divs(class: "offer-card")

    @products.each do |a|
        # if Ibotta.find_by title: a.imgs[0].alt

        if a.divs[2].text.split("").include?('%')

        else
            value_placeholder = a.divs[3].text.split(" ")
            value_placeholder.delete("cash")
            value_placeholder.delete("back")
            value_placeholder = value_placeholder.join(" ").split("")
            value_placeholder.delete("$")
            value_placeholder = value_placeholder.join("")

            Ibotta.create(title: a.imgs[0].alt, values: value_placeholder, store: a.divs[5].text, link: a.links[0].href)
        end
    end
    @products = Ibotta.all
end


def show
    @products = Ibotta.all
end


def delete
    Ibotta.delete_all
    @products = Ibotta.all
end


def practice

end

end

View

<h1>Show Page for iBotta</h1>

<h3><%= @products.length %> products in the iBotta DB</h3>

<% @products.each do |x| %>
    <p>Title: <a href=<%=x.link%>><%= x.title %></a> </p> 
    <p>Value: <%= x.values %> </p>
    <p>Store: <%= x.store %> </p>
<% end %>

If you also have advice on what code I need to add, could you mention what file to add the code in? Thanks.

Routes

Rails.application.routes.draw do
  resources :articles

  get 'scraper/ibotta'
  get 'scraper/checkout51'
  get 'ibotta/save'
  get 'ibotta/show'
  get 'ibotta/delete'
  get 'targetcoupon/save'
  get 'targetcoupon/delete'
  get 'targetcoupon/show'
  get 'targetibottum/delete'
  get 'targetibottum/show'
  get 'targetibottum/save'
  get 'savingstar/delete'
  get 'savingstar/save'
  get 'savingstar/show'
  get 'ibottasavingstar/show'
  get 'ibottasavingstar/save'
  get 'ibottasavingstar/delete'
  get 'targetcoupon/practice'
  get 'targetibottasavingstar/show'
  get 'targetibottasavingstar/save'
  get 'targetibottasavingstar/delete'
  get 'checkout51/save'  
  get 'checkout51/show' 
  get 'checkout51/delete'
  get 'checkout51/practice'

  get 'ibotta/practice'

  get 'ibottacheckout51/save'  
  get 'ibottacheckout51/show' 
  get 'ibottacheckout51/delete'

  get 'ibottacheckout51/practice'  

  get 'newcheckout51/save'  
  get 'newcheckout51/show' 
  get 'newcheckout51/delete'

  get 'smiths/save'
  get 'smiths/show'
  get 'smiths/delete'
  get 'smiths/practice'
3
  • 1
    Show output of rails routes (or rake routes in older Rails versions). Deletion code looks fine (tho delete_all won't trigger any callbacks), so all You got to do is route it correctly. But are You sure You need to assign @products in delete action? Looks like You are using get request to delete, but It should be delete one, and in delete action after Ibotta.delete_all You probably should redirect to other path instead of assigning products and rendering delete view Commented Sep 7, 2017 at 20:30
  • I want to add a button to delete products individually, not all at once. Commented Sep 7, 2017 at 20:31
  • As @JakubKopyś suggested, can you show the rake routes output? generally for deleting some entry you would have to send params,so that rails can select & delete that particular entry (product) for eg.for deleting a particular comment (same as deleting a particular product):- <%= link_to 'delete',"/comments/#{comment.id}",method: :delete, data: { confirm: 'Are you sure?' } %> PS:You can get the link_to path from rake routes Commented Sep 7, 2017 at 20:57

1 Answer 1

2

Why don't You want to use params? I do not know If It is even possible...

With ID You could simply add something like <%= link_to 'delete', ibotta_path(x.id), method: :delete %> In Your view. If You have resources routes the path helper should be avaliable for You. Then in controller add:

    def destroy
      Ibotta.find(params[:id]).destroy
      redirect_to your_redirect_path
    end

EDIT: I see that You are not using resources routing - add delete 'ibotta/:id', to: 'ibotta#destroy' to Your routes.rb or just use resources routing

So Your view would look like:

<% @products.each do |x| %>
    <p>Title: <a href=<%=x.link%>><%= x.title %></a> </p> 
    <p>Value: <%= x.values %> </p>
    <p>Store: <%= x.store %> </p>
    <p><%= link_to 'delete', ibotta_path(x.id), method: :delete %></p>
<% end %>

One note - I think You shouldn't use variable names like 'x' in each block, use 'product' instead, it is much more descriptive.

Sign up to request clarification or add additional context in comments.

3 Comments

So I added the new code that goes in the view and for the routes I added resources :ibotta And when I click on the delete button it redirects me to a new page but it doesn't delete the product. Thoughts?
hmmmm... maybe Ibotta.find(params[:id]).destroy! - should throw exception if it fails, then You can check out server logs to see what failed. Feel free to share error message (if any),
Added it, but am getting nothing in the console. This was a big help though and a good start, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.