轨道上的红宝石 - 找不到 ID = 活动记录::记录未找到的"Object"



每次我删除或销毁产品时,它总是在控制台中给我这个错误:

ActiveRecord::RecordNotFound (Couldn't find Product with ID=4):
  app/controllers/products_controller.rb:16:in `show'

在页面上显示:

ActiveRecord::RecordNotFound in ProductsController#show
Couldn't find Product with ID=4

在我的产品控制器中,我只有一个常规的支架:

def show
    @product = Product.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @product }
    end
  end
def destroy
    @product = current_user.products.find(params[:id]) #current user deletes own
    @product.destroy
    respond_to do |format|
      format.html { redirect_to(products_url) }
      format.xml  { head :ok }
    end
  end

我使用Rails Jquery UJS和Rails - Jquery,并在我的应用程序布局的csrf_meta_tag。我注意到的是,当我点击破坏链接,它弹出窗口,说"你确定吗",我点击OK按钮,它闪烁两次,因为一些奇怪的原因,它从来没有这样做,直到我安装Jquery。我怎么解决这个问题?

EDIT - ANSWER:

安装rails jquery-ujs> https://github.com/rails/jquery-ujs

你必须保留jquery.min.jsjquery.js两个文件。我删除了jquery.min.js,因为我认为这是一个最小化的版本,但显然不是,这里是我所有的文件从jquery-ujs:

   jquery.js
   jquery.min.js
   jquery_ujs.js
   rails.js # i had to install this manually from the link (or zip file)
   jquery-ui # i wanted the user interface too

确保在javascripts中有rails.js文件?当您从原型转移到jquery时,标准的restful DELETE不再正常工作了。Rails.js修复了这个问题,使其再次正常工作。你可以参考Josh Huckabee的文章:http://joshhuckabee.com/jquery-rails-3。希望这对你有帮助。

PS:还要确保在你的布局中包含rails.js。像这样:

<%= javascript_include_tag 'jquery-1.4.4.min', 'jquery-ui-1.8.7.custom.min', 'rails', 'application' %>

错误消息说您正在尝试在show操作中找到具有id 4的product。这里有两个问题。首先,除非你的产品id真的是4,否则你可能会从params[:id]中获得nil。另一个问题是,你在你的show行动,而不是你的delete行动。这意味着你的路由是错误的,或者http动词从DELETE更改为GET

闪烁两次可能是一个相关的症状,但你没有发送DELETEparams哈希没有被传递。

最新更新