我最近为我的路线添加了本地化范围(如 Railscasts PRO #138 中所述),因为我需要它们看起来像这样:
localhost/en/admin/posts
localhost/en/admin/posts/happy-new-post/edit
但是添加范围会引发错误:
can't find record with friendly id: "posts"
这是我的参数在这种情况下的外观:
Request
Parameters:
{"page_category_id"=>"admin", "id"=>"posts"}
这是我的 config/routes.rb 的样子:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
namespace :admin do
get '', to: 'dashboard#index', as: '/'
namespace :settings do
get '', to: 'dashboard#index', as: '/'
resources :general_settings, except: :show
end
resources :pages, except: :show
resources :posts, except: :show
resources :page_categories, except: :show
resources :post_categories, except: :show
resources :users, except: :show
end
end
end
如果有帮助,这是我的后期模型:
class Post < ApplicationRecord
include Bootsy::Container
belongs_to :post_category
validates :title, presence: true
extend FriendlyId
friendly_id :title, use: [:finders, :slugged]
translates :title, :body
globalize_accessors :locales => [:en, :ru], :attributes => [:title, :body]
end
有没有办法让 url 像localhost/en/admin/posts/happy-new-post/edit
工作一样?