在 rails 应用程序上实施 friendly_id 后,旧网址仍然有效。我怎样才能阻止他们并重定向到 404?
Old urls are still working after implementing friendly_id on rails app. How can I stop them and redirect to 404?
我在我的 rails 应用程序上实现了 friendly_id
。之前单击 link 时,url 曾经如下所示:
https://example.com/search?category_id=228
执行 friendly_id
后,当 link 被点击时,它开始给出 url 如下:
https://example.com/search?category_id=construction-and-heavy-equipment
我在平台上实现了一个404
方法。当给出无效的 urls 时,它会重定向到 404
错误页面。它适用于无效的 urls.
但是现在当我测试给旧的url即https://example.com/search?category_id=228
时它仍然有效。我该如何阻止它?
控制器
def search_equipments
begin
if (params.keys & ['category_id', 'sub_category', 'manufacturer', 'country', 'state', 'keyword']).present?
if params[:category_id].present?
@category = Category.active.find params[:category_id]
else
@category = Category.active.find params[:sub_category] if params[:sub_category].present?
end
@root_categories = Category.active.roots
@sub_categories = @category.children.active if params[:category_id].present?
@sub_categories ||= {}
@countries = Country.active.all
@manufacturers = Manufacturer.active.all
@states = State.active.where("country_id = ?", params[:country]) if params[:country].present?
@states ||= {}
unless params[:category_id].present? && params[:sub_category].present?
params[:category_id] = @category.id if params[:category_id].present?
params[:sub_category] = @category.id if params[:sub_category].present?
end
@equipments = Equipment.active.filter(params.slice(:manufacturer, :country, :state, :category_id, :sub_category, :keyword)).order("#{sort_column} #{sort_direction}, created_at desc").page(params[:page]).per(per_page_items)
else
redirect_to root_path
end
rescue Exception => e
redirect_to root_path, :notice => "Something went wrong!"
end
end
担忧
module Filterable
extend ActiveSupport::Concern
module ClassMethods
def filter(filtering_params)
results = self.where(nil)
filtering_params.each do |key, value|
results = results.public_send(key, value) if value.present?
end
results
end
end
end
根据friendly_id
源代码,你应该使用find_by_friendly_id
。因为 find 方法首先通过 slug 查询,然后回退到常规 find 方法。
# If the id is "unfriendly", it will call the original find method.
# If the id is a numeric string like '123' it will first look for a friendly
# id matching '123' and then fall back to looking for a record with the
# numeric id '123'.
#
# Since FriendlyId 5.0, if the id is a numeric string like '123-foo' it
# will *only* search by friendly id and not fall back to the regular find
# method.
#
# If you want to search only by the friendly id, use {#find_by_friendly_id}.
更改代码
@category = Category.active.find params[:category_id]
至此
@category = Category.active.friendly.find_by_friendly_id params[:category_id]
并检查 https://github.com/norman/friendly_id/issues/648#issuecomment-75172624,这是另一个解决方案,但更复杂。
您可以通过两种方式做到这一点:
您可以发送 slug
.
而不是从视图中发送 id
如果您在请求中找到数字 id
,其他方法是在 controller
中设置一些 redirect
代码。
我在我的 rails 应用程序上实现了 friendly_id
。之前单击 link 时,url 曾经如下所示:
https://example.com/search?category_id=228
执行 friendly_id
后,当 link 被点击时,它开始给出 url 如下:
https://example.com/search?category_id=construction-and-heavy-equipment
我在平台上实现了一个404
方法。当给出无效的 urls 时,它会重定向到 404
错误页面。它适用于无效的 urls.
但是现在当我测试给旧的url即https://example.com/search?category_id=228
时它仍然有效。我该如何阻止它?
控制器
def search_equipments
begin
if (params.keys & ['category_id', 'sub_category', 'manufacturer', 'country', 'state', 'keyword']).present?
if params[:category_id].present?
@category = Category.active.find params[:category_id]
else
@category = Category.active.find params[:sub_category] if params[:sub_category].present?
end
@root_categories = Category.active.roots
@sub_categories = @category.children.active if params[:category_id].present?
@sub_categories ||= {}
@countries = Country.active.all
@manufacturers = Manufacturer.active.all
@states = State.active.where("country_id = ?", params[:country]) if params[:country].present?
@states ||= {}
unless params[:category_id].present? && params[:sub_category].present?
params[:category_id] = @category.id if params[:category_id].present?
params[:sub_category] = @category.id if params[:sub_category].present?
end
@equipments = Equipment.active.filter(params.slice(:manufacturer, :country, :state, :category_id, :sub_category, :keyword)).order("#{sort_column} #{sort_direction}, created_at desc").page(params[:page]).per(per_page_items)
else
redirect_to root_path
end
rescue Exception => e
redirect_to root_path, :notice => "Something went wrong!"
end
end
担忧
module Filterable
extend ActiveSupport::Concern
module ClassMethods
def filter(filtering_params)
results = self.where(nil)
filtering_params.each do |key, value|
results = results.public_send(key, value) if value.present?
end
results
end
end
end
根据friendly_id
源代码,你应该使用find_by_friendly_id
。因为 find 方法首先通过 slug 查询,然后回退到常规 find 方法。
# If the id is "unfriendly", it will call the original find method.
# If the id is a numeric string like '123' it will first look for a friendly
# id matching '123' and then fall back to looking for a record with the
# numeric id '123'.
#
# Since FriendlyId 5.0, if the id is a numeric string like '123-foo' it
# will *only* search by friendly id and not fall back to the regular find
# method.
#
# If you want to search only by the friendly id, use {#find_by_friendly_id}.
更改代码
@category = Category.active.find params[:category_id]
至此
@category = Category.active.friendly.find_by_friendly_id params[:category_id]
并检查 https://github.com/norman/friendly_id/issues/648#issuecomment-75172624,这是另一个解决方案,但更复杂。
您可以通过两种方式做到这一点:
您可以发送
slug
. 而不是从视图中发送 如果您在请求中找到数字
id
,其他方法是在controller
中设置一些redirect
代码。
id