Ruby on rails Rails-向路由添加作用域会破坏友好的URL
我最近在我的路线中添加了本地化范围(如Railscasts PRO#138中所述),因为我需要它们看起来像这样:Ruby on rails Rails-向路由添加作用域会破坏友好的URL,ruby-on-rails,ruby,friendly-url,globalize,Ruby On Rails,Ruby,Friendly Url,Globalize,我最近在我的路线中添加了本地化范围(如Railscasts PRO#138中所述),因为我需要它们看起来像这样: localhost/en/admin/posts localhost/en/admin/posts/happy-new-post/edit 但添加作用域会引发错误: 找不到具有友好id的记录:“posts” 下面是我的助手们在这件事上的看法: Request Parameters: {"page_category_id"=>"admin", "id"=>"posts
localhost/en/admin/posts
localhost/en/admin/posts/happy-new-post/edit
但添加作用域会引发错误:
找不到具有友好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
class Post[:en,:ru],:attributes=>[:title,:body]
结束
有没有办法让像
localhost/en/admin/posts/happy new post/edit这样的url正常工作?如果将其更改为scope path:':locale',…
,是否有帮助?语法通常也是:scope:locale”,约束:{locale://{I18n.available_locales.join(“|”)}/}do
。我还发现查看rake routes
@mohnstrudel的输出很有帮助,因为您的代码看起来不错。这些参数的请求URL是什么
{“page\u category\u id”=>“admin”,“id”=>“posts”}?@MarcRohloff-Marc,不幸的是它没有帮助。我的rails-routes
输出有点疯狂,我最好截屏,而不是发表评论-@bgs-bgs,简单地http://localhost:3000/admin/posts/
这是一个完整的屏幕截图,如果有帮助的话-实际上,我通过在会话中保存所选的区域设置来解决问题。我在某个地方看到,强烈建议用户清楚地了解当前选择的区域设置。现在,对于我的管理部分来说,这并不会让我太烦恼,但我很快会再次遇到这个问题:)你确定路由文件中没有其他冲突吗?通常,参数将包括控制器
和操作
参数。如果省略作用域路径:':locale'
部分,它们确实包括这些参数。或者如果我移除了友好id宝石。这些是我在另一个项目中使用globalize和scope
part:root GET/:locale(:format)front/static#pages#home{:locale=>/en | ru/}admin GET/:locale/admin(:format)admin/dashboard#index{:locale=>/en | ru/}admin_employees GET/:locale/admin/employees(:format)admin/employees#index{:locale=>/en | ru/}