Reputation: 459
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'
Upvotes: 2
Views: 126
Reputation: 710
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.
Upvotes: 2