gpt4 book ai didi

ruby-on-rails - RoR 如何获取/needs/4 而不是/needs.4 的button_to 路径?

转载 作者:行者123 更新时间:2023-12-04 06:23:11 26 4
gpt4 key购买 nike

我对 RoR 还是很陌生,我正在尝试使用 button_to delete 按钮删除一个对象。但是,使用我编写的代码,当我尝试将它获取到 destroy 方法的/needs/:id 时,它会将我带到/needs.4 而不是/needs/4 。通过需求 Controller 和需求 new.html.erb 页面创建“需求”,然后显示在用户的显示页面中。从那里,用户应该能够删除他/她的需求。这是我得到的错误:

ActiveRecord::RecordNotFound in NeedsController#destroy

Couldn't find Need with id=@userneed
Rails.root: /Users/mcn/Dropbox/Code/GA/Projects/GA_projects/p4_final/flatcircle

Application Trace | Framework Trace | Full Trace
app/controllers/needs_controller.rb:20:in `destroy'
Request

Parameters:

{"_method"=>"delete",
"authenticity_token"=>"Fv6EcMNJQEjtw1naQVMw77lkCGjTJR7ui2FD53aoZfc=",
"id"=>"@userneed"}

这是我的代码:

需要 Controller :

def destroy
Need.find(params[:id]).destroy
redirect_to :controller => :users, :action => :show, :id => current_user.id, :flash => { :success => "Your search post was deleted." }
end

用户显示页面button_to行:

  <%= button_to "delete", '/needs/@userneed', method: :delete, data: { confirm: "You sure?"} %>

在同一页上:

@userneed = @current_user.needs.last

Routes.rb

delete "/needs/:id", to: "needs#destroy"
get "/needs/:id", to: "needs#show"

super 迷茫,知道怎么解决的请告诉我!

最佳答案

好的,这就是我修复它的方法:

<%= button_to "delete", {:controller => :needs, :action => "destroy", :id => current_user.needs.last.id}, :method => :delete, data: { confirm: "You sure?"} %>

所以我猜这是两件事:1)正确位置的花括号(我需要它们,因为在花括号中的内容之后还有争论2) 以这种方式指定 id 而不是使用 _path() 方式

关于ruby-on-rails - RoR 如何获取/needs/4 而不是/needs.4 的button_to 路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20761367/

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