当我尝试编辑使用 friendly_id 的记录时,它给我一个无路由匹配错误
When I try to edit a record which use friendly_id, it gives me a no route match error
我正在为我的产品模型使用 friendly_id。这是我的路线:
resources :products, only: :show
scope '/dash' do
resources :products
end
现在,当我进行编辑操作时,我使用了友好的 ID 来加载正确的表单。编辑表单正确加载。但是当我提交它时,它给出了这样的错误:
No route matches [PATCH] "/products/P011700714"
我该如何解决这个问题?
标签形式:
<%= form_for( edit_product_path(@product) ) do |f| %>
按照这些步骤,
从你的路线中删除 resources :products, only: :show
因为这是导致问题的原因,因为你只需要 show
没有范围的操作我建议你添加这一行而不是resources :products, only: :show
get '/products/:id', to: 'products#show'
将您的 form_for
标签更改为
<%= form_for @product do |f| %>
使用slug
获取记录
def edit
@product = Product.where("slug=? or id=?", params[:id], params[:id]).first
# your code
end
希望对您有所帮助!
请关注下面this link,我相信你会得到一些有用的
请找到我的代码:
class User < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
end
资源:用户
我正在为我的产品模型使用 friendly_id。这是我的路线:
resources :products, only: :show
scope '/dash' do
resources :products
end
现在,当我进行编辑操作时,我使用了友好的 ID 来加载正确的表单。编辑表单正确加载。但是当我提交它时,它给出了这样的错误:
No route matches [PATCH] "/products/P011700714"
我该如何解决这个问题?
标签形式:
<%= form_for( edit_product_path(@product) ) do |f| %>
按照这些步骤,
从你的路线中删除
resources :products, only: :show
因为这是导致问题的原因,因为你只需要show
没有范围的操作我建议你添加这一行而不是resources :products, only: :show
get '/products/:id', to: 'products#show'
将您的
form_for
标签更改为<%= form_for @product do |f| %>
使用
获取记录slug
def edit @product = Product.where("slug=? or id=?", params[:id], params[:id]).first # your code end
希望对您有所帮助!
请关注下面this link,我相信你会得到一些有用的
请找到我的代码:
class User < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
end
资源:用户