質問

My problem is that I need to add the option for delete product and I'm stock with that....

First I added this line in the index.html.erb:

<% glyph_to "Delete", product, method: :delete, data: (confirm: "Are you sure that product" ##(product.id) is not used?") if can? (:destroy, product)%>

but I don't now in that line what is the difference between "method: :delete" and "if can? (:destroy, product)", destroy is the controller action I guess and delete? I don't know....

another thing is my controller definition for that action:

def destroy 
    @product = Product.find(params[:id])
    @product.destroy
    redirect_to product_path
end

but when I press "delete" it doesn't redirect to product_path

and my routes definition are the following:

resources products do 
    member do
        get :destroy
    end
end

I will be very grateful of any help with this,

thank you for your time :D!

役に立ちましたか?

解決 3

destroy action must be called on HTTP DELETE request and never on GET. You should definitely fix the following code from your routes:

resources products do  ## missing : here, it should be :products
  ## member block not required
  member do  
      get :destroy  ## destroy should never be GET request
  end
end

You just need to define your routes as:

resources :products ## Notice :products and not products

This will generate following routes for you which you can check by running command rake routes

         products GET    /products(.:format)                   products#index
                  POST   /products(.:format)                   products#create
      new_product GET    /products/new(.:format)               products#new
     edit_product GET    /products/:id/edit(.:format)          products#edit
          product GET    /products/:id(.:format)               products#show
                  PATCH  /products/:id(.:format)               products#update
                  PUT    /products/:id(.:format)               products#update
                  DELETE /products/:id(.:format)               products#destroy

Notice the last route generated. destroy action should be called as HTTP Delete method which is why on your link you need to specify method: :delete as

<% glyph_to "Delete", product, method: :delete, data: (confirm: "Are you sure that product" ##(product.id) is not used?") if can? (:destroy, product)%>

so when you click on this link a DELETE request would be sent which would be mapped to your route for products#destroy, i.e.,

   DELETE /products/:id(.:format)               products#destroy

Also, you need to update the destroy action as below:

def destroy 
    @product = Product.find(params[:id])
    @product.destroy
    redirect_to products_url  ## products_url notice plural products
end

products_url would redirect you to index page of products. As product_path is for show, i.e., to show a particular product. You can't show a deleted product that too without providing an id to product_path. So, logically you should be redirecting to index action with products_url (NOTE using **_url for redirection is advisable but you could also use products_path)

他のヒント

Yes , destroy is controller's delete method , to redirect to the product listing page you should use redirect_to action: 'index'. You can check all actions and method through command rake routes

You should redirect_to products_path(this translates to index)
since currently it says redirect_to product path(translating to show) which will throw error
method: :delete is for posting to destroy action and can? is a cancan gem method by Ryan Bates. The if checks if user has the permissions to delete the product or not.

The permissions can be set in app/models/ability.rb

Lastly change your routes to:

resources: :products
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top