0

I am using friendly_id for my Product model. Here's my routes :

resources :products, only: :show

scope '/dash' do
   resources :products
end

Now when I go to the edit action, I used friendly ID to load the correct form. The edit form loads correctly. But when I submit it, it gives an error like this :

No route matches [PATCH] "/products/P011700714"

How can I fix this?

The form for tag :

<%= form_for( edit_product_path(@product) ) do |f| %>
Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
THpubs
  • 7,804
  • 16
  • 68
  • 143

2 Answers2

1

Follow these steps,

  1. Remove resources :products, only: :show from your routes as that is what causing the problem, as you only need show action without the scope I would recommend you to add this line instead of resources :products, only: :show

    get '/products/:id', to: 'products#show'
    
  2. Change your form_for tag to

    <%= form_for @product do |f| %>
    
  3. Fetch the record using slug

    def edit
      @product = Product.where("slug=? or id=?", params[:id], params[:id]).first
      # your code
    end
    

Hope this helps!

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
  • Actually I don't want the show action. I want to go to `update` – THpubs Mar 20 '15 at 07:14
  • Isn't it possible to do it by keeping the rest of the product actions under /dash like in my routes file? – THpubs Mar 20 '15 at 07:24
  • Yes you can do that, edit your question and add routes for the `dash` scope, that would help me answering the question – Rajdeep Singh Mar 20 '15 at 07:25
  • It's already there in the original question `scope '/dash' do resources :products end` – THpubs Mar 20 '15 at 07:39
  • No I asked you to add results of the `rake routes`, anyways no need to do that now as I have already edited my answer again. This should definitely solve your problem! – Rajdeep Singh Mar 20 '15 at 07:43
  • 1
    Perfect! Thanks a lot! That worked. Removing resources and adding only get for show action solved the problem! – THpubs Mar 20 '15 at 07:48
0

Please follow below this link , i am sure you will get some useful

Please find my code:

class User < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged
end

resources :users

ROR
  • 441
  • 2
  • 13