【发布时间】:2014-08-05 10:00:23
【问题描述】:
我现在正在做网站 SEO 优化,我需要做的是正确的路由,以使链接对 seo 非常友好。我已经阅读了很多关于路由的信息,但它在我的脑海中一团糟,我卡住了。
所以我有属于 StoreType 模型的 Store 模型,属于 City 模型和 District 模型 + District belongs_to :city。
我需要这样的路线:
/stores/store_type_name/ - store_type 'show' action(list of stores by type)
/stores/city_name/store_type_name/ - store_type 'show' action(list of stores by city&type)
/stores/city_name/district_name/store_type_name/ - store_type 'show' action(list of stores by city&district&type)
/stores/city_name/store_type_name/store_name - store 'show' action
我现在想出的唯一解决方案是: 路线.rb
namespace :stores do
get ':transliterated', to: 'store_types#show'
get ':transliterated/:name_en', to: 'store_types#city'
get ':transliterated/:name_en/:id', to: 'store_types#district'
end
使用这样的控制器:
def district
@store_type = StorerType.find_by_transliterated(params[:transliterated])
@city = City.find_by_name_en(params[:name_en])
@district = District.find_by_id(params[:id])
if @store_type && @city && @district
stores = @store_type.stores.where(city_id:@city.id)
@stores = stores.where(district_id:@district.id)
else
redirect_to root_path
end
end
效果很好,但是 1) 我现在无法为最后一个示例添加路由(存储显示页面),因为路由正在该命名空间中寻找 :transliterated 参数,如果找不到记录则重定向。 2)我知道这个解决方案很糟糕,可以做得更好,我只是不知道怎么做。请给我一个建议。
PS。实际上网站上已经实现了路由,所以我只为上面列出的这 4 个 url 寻找解决方案,没有触及其他任何东西。
【问题讨论】:
标签: ruby-on-rails url routing seo