Search code examples
ruby-on-railsruby-on-rails-4friendly-id

When I try to edit a record which use friendly_id, it gives me a no route match error


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| %>

Solution

  • 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!