【问题标题】:simple_form_for with localized, scoped routes and Mongoid (missing required keys: [:id])simple_form_for 具有本地化、范围路由和 Mongoid(缺少必需的键:[:id])
【发布时间】:2015-03-12 14:24:04
【问题描述】:

我有一个模型FileType_form.html.haml 部分,我想在我的new.html.hamledit.html.haml 视图中呈现它。

= simple_form_for [:document_manager, file_type] do |f|
    ...

当我在 new 视图中渲染这个部分时,这是可行的,但是当我在 edit 视图中渲染它时,simple_form 尝试设置 id file_typelocale 参数如果我的路线。我的路线如下所示:

Rails.application.routes.draw do
  scope '(:locale)', locale: locale: /#{I18n.available_locales.join('|')}/ do
    namespace :document_manager do
      resources :file_types, except: [:show]
    end
  end
end

对于 new 视图,路由 /en/document_manager/file_types 已生成,但当我尝试访问 edit 视图时出现此错误:

No route matches {
  :action=>"update", 
  :controller=>"document_manager/file_types", 
  :format=>nil, 
  :id=>nil, 
  :locale=>#<FileType _id: 550198ca7462720388010000, user_file_id: nil, file_name: "...", description: "...">
} missing required keys: [:id]

如何更改为newedit正确设置idlocale参数的表单?

更新:第一个可行的解决方案是删除 locale 范围并将语言存储在会话中,但对于 SEO,这将导致重复内容,因此更好的解决方案是有这样的网址:

  • /zh/document_manager/file_types
  • /fr/document_manager/file_types
  • /de/document_manager/file_types
  • ...

【问题讨论】:

    标签: ruby-on-rails mongoid simple-form rails-i18n


    【解决方案1】:

    使用普通路径,有时 rails 过于复杂,在这种情况下,它应该自动使用您正在使用的范围,但是当您尝试使用它时,我只使用了硬编码范围,例如 /en|es|fr/

    如果我是你,我会使用类似的东西:

    = simple_form_for document_file_type_path, method: :post do |f|
    

    并使用 2 种不同的形式,尝试对可用的语言环境进行硬编码,例如

    scope "(:locale)", :locale => /en|es|fr/ do
      resources :foos
      resources :bars
      ...
    

    编辑

    看起来也是在等待3个参数试试:

    [:document_manager, "locale", file_type]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      相关资源
      最近更新 更多