重新开始编程,但我遇到了这个基本的问题。所以我从网站上删除了产品,然后将它们插入到数据库中。然后我在我的网站上列出这些产品。现在,我正在尝试在我的网站上列出的每个产品旁边添加一个删除按钮。我已经尝试使用stackoverflow上找到的解决方案,但我似乎无法让它们中的任何一个工作。我知道这是一个基本问题,但我很感激帮助。
控制器
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
查看
<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 %>
如果您对我需要添加的代码有建议,您能提一下要添加代码的文件吗?谢谢。
路线
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'
答案 0 :(得分:2)
为什么不想使用params?我不知道是否有可能......
使用ID您只需在视图中添加<%= link_to 'delete', ibotta_path(x.id), method: :delete %>
之类的内容即可。
如果您有资源路由,则路径助手应该可以为您提供。
然后在控制器中添加:
def destroy
Ibotta.find(params[:id]).destroy
redirect_to your_redirect_path
end
编辑:我发现您没有使用资源路由 - 将delete 'ibotta/:id', to: 'ibotta#destroy'
添加到routes.rb
或仅使用resources routing
所以你的观点看起来像是:
<% @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 %>
一个注意事项 - 我认为您不应该使用变量名称,例如&#39; x&#39;在每个区块中,使用&#39; product&#39;相反,它更具描述性。