lundi 15 octobre 2018

Rails: Locales using form_with?

How do I use locales and form_with? By locale I mean /en instead of /?locale=en.

This is part of my view file:

<%= form_with(model: @model, local: true, locale: I18n.locale) do |form| %>

After submitting the form I get the following error:

No route matches {:action=>"show", :controller=>"model_name", :format=>nil, :locale=>#<ModelName id: 2, created_at: "2018-10-15 11:07:36", updated_at: "2018-10-15 11:08:09">}, missing required keys: [:id], possible unmatched constraints: [:locale]

Notice how locale is set with the given model ...

This is my routes.rb:

root 'model_name#new'

scope ':locale', locale: /[a-z]{2}/ do
  root 'model_name#new'
  resources :model_name, only: [:create, :show]
end

I don't understand why I need to set root twice but ...

Thanks!

Aucun commentaire:

Enregistrer un commentaire