开发者

Couldn't find "Object" with ID = ActiveRecord::RecordNotFound

开发者 https://www.devze.com 2023-03-18 07:32 出处:网络
Every time i delete or destroy a Product it aways give me this error in my console: ActiveRecord::RecordNotFound (Couldn\'t find Product with ID=4):

Every time i delete or destroy a Product it aways give me this error in my console:

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

and on the page it gives:

ActiveRecord::RecordNotFound in ProductsController#show

Couldn't find Product with ID=4

In my Products Controller i just have the regular scaffold:

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

I am using Rails Jqu开发者_高级运维ery UJS and rails-jquery and have the csrf_meta_tagin my app layout. What i have noticed is when i click the destroy link and it pops up the window and says "Are you Sure", i click the OK button and it flickers TWICE for some strange reason, it never did this until i installed Jquery. How do i fix this?

EDIT - ANSWER:

Reinstall rails jquery-ujs > https://github.com/rails/jquery-ujs

You have to keep both files jquery.min.js and jquery.js. I deleted the jquery.min.js because i thought it was a minimized version but apparently not, here are all of my files from 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


Make sure you have the rails.js file in javascripts? When you move from prototype to jquery, the standard restful DELETE doesn't work properly anymore. rails.js fixes that so it works normally again. You can refer to Josh Huckabee's article here: http://joshhuckabee.com/jquery-rails-3. Hopefully that helps.

PS: Also make sure you include rails.js in your layout. Something like this:

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


The error message says that you are trying to find a product with id 4 in your show action. There are two issues there. The first is that unless your product's id really is 4, you are probably getting nil from params[:id]. The other problem is that you are in your show action and not your delete action. This implies that either your routing is wrong or that the http verb is being changed from a DELETE to a GET.

The flickering twice maybe a related symptom, but some how you aren't sending a DELETE and the params hash is not being passed.

0

精彩评论

暂无评论...
验证码 换一张
取 消