gpt4 book ai didi

ruby-on-rails - 如何在不使用参数的情况下添加按钮来删除数据?

转载 作者:数据小太阳 更新时间:2023-10-29 08:57:42 24 4
gpt4 key购买 nike

重新开始编程,但我在处理这个基本问题时遇到了麻烦。所以我从网站上抓取了产品,然后将它们插入到数据库中。然后我在我的网站上列出这些产品。现在我想在我网站上列出的每个产品旁边添加一个删除按钮。我试过使用在 stackoverflow 上找到的解决方案,但我似乎无法让它们中的任何一个工作。我知道这是一个基本问题,但我感谢您的帮助。

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

查看

<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'

最佳答案

你为什么不想使用参数?我不知道是否有可能...

使用 ID 您可以简单地添加类似 <%= link_to 'delete', ibotta_path(x.id), method: :delete %> 的内容在您看来。如果您有资源路由,路径助手应该对您可用。然后在 Controller 中添加:

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

编辑:我看到您没有使用资源路由 - 添加 delete 'ibotta/:id', to: 'ibotta#destroy'给你的routes.rb或者只使用 resources routing

所以你的 View 看起来像:

<% @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 %>

请注意 - 我认为您不应该在每个 block 中使用像“x”这样的变量名,而是使用“product”,它更具描述性。

关于ruby-on-rails - 如何在不使用参数的情况下添加按钮来删除数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46104439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com