Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path for form helper #105

Closed
D4v1dW3bb opened this issue May 18, 2020 · 7 comments
Closed

path for form helper #105

D4v1dW3bb opened this issue May 18, 2020 · 7 comments

Comments

@D4v1dW3bb
Copy link

When making a form i got an error:

ActionView::Template::Error (undefined method 'i18n_backend_active_record_translation_path' for #<#<Class:0x00007f04a9399298>:0x00007f04917462c8>):
when adding url: translations_path to the form it worked again.
I think this originates from the initializer line:
Translation = I18n::Backend::ActiveRecord::Translation
There for Translation.name and Translation.sti_name is `I18n::Backend::ActiveRecord::Translation'

So when you call a form without an url parameter i18n_backend_active_record_translation_path is automatically generated for url.

Would be nice to ad a comment in the readme to put add "url: <translation_view_dir>_path" to form_with

@timfjord
Copy link
Collaborator

In general, that fact that the i18n_backend_active_record_translation_path is not defined but the translations_path is has nothing to do with the initializer.
translations_path works only because you have an entry in routes.rb that defines a route named translations, so if you add something like

get '/something' => 'my_controller#my_action', as: :i18n_backend_active_record_translation

most likely it will work without url: translations_path

You can also try

class Translation < I18n::Backend::ActiveRecord::Translation
end

Adding the note about the path helper might be confusing because this gem doesn't really add routes and controllers and acts purely as a backend for I18n gem.

@D4v1dW3bb
Copy link
Author

D4v1dW3bb commented May 19, 2020 via email

@timfjord
Copy link
Collaborator

It is not recomennded to keep class Translation < I18n::Backend::ActiveRecord::Translation in the initializer, it should be in app/models/translation.rb
Basically this way we define our own AR model but inherit from the model from the gem.
I shared that just to illustrate that the issue wasn't really related to the initializer.

It is expected that I18n::Backend::ActiveRecord::Translation returns i18n_backend_active_record_translation_path.
Usage of the model in helpers like form_for or link_to is not supported because this gem doesn't provide any controllers, routes to handle that.

@D4v1dW3bb
Copy link
Author

It is expected that I18n::Backend::ActiveRecord::Translation returns i18n_backend_active_record_translation_path.
Usage of the model in helpers like form_for or link_to is not supported because this gem doesn't provide any controllers, routes to handle that.

The gem does not provide it but it's expected the users of the gem are going to use it. Their not going to fill the translation table by hand. so they will need to make controllers, views and routes. Therefor the translation table should act like every other model whether it's installed by a gem or the programmer implements it himself.

@timfjord
Copy link
Collaborator

You can use i18n_backend_active_record_translation name in your routes because this is what the model generates.

But I am going to revisit Translation = I18n::Backend::ActiveRecord::Translation part in the initializer and probably include class Translation < I18n::Backend::ActiveRecord::Translation model instead.

@floydj
Copy link

floydj commented Jan 30, 2024

You can use i18n_backend_active_record_translation name in your routes because this is what the model generates.

So how exactly is this done?

@timfjord
Copy link
Collaborator

timfjord commented Jan 30, 2024

You can use i18n_backend_active_record_translation name in your routes because this is what the model generates.

So how exactly is this done?

The easiest way is to have something like this in the routes.rb:

post '/some_path', to: 'my_controller#my_action', as: 'i18n_backend_active_record_translation'

But the custom model should be more convinient

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants