我对 RoR 还是很陌生,我正在尝试使用 button_to 删除按钮删除一个对象。但是,使用我编写的代码,当我尝试将它带到 /needs/:id 的 destroy 方法时,它会让我到达 /needs.4 而不是 /needs/4。“需求”通过需求控制器和需求 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"}
这是我的代码:
需求控制器:
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
路由.rb
delete "/needs/:id", to: "needs#destroy"
get "/needs/:id", to: "needs#show"
超级困惑,如果您知道如何解决,请告诉我!