Rails action_form has a damaged locale

I have an account panel that lists offices and jobs are available for each office.

Hierarchy:

  • Companies (1 user has 1 company, I have access if from the user profile)
  • Offices (each company may have several offices)
  • Work (each office can have several tasks)

Models:

class Company < ActiveRecord::Base has_many :offices, :dependent => :destroy has_many :jobs, :through => :offices class Office < ActiveRecord::Base belongs_to :company has_many :jobs, :dependent => :destroy class Job < ActiveRecord::Base belongs_to :office 

For every job I have an edit link

eg. for job id 10 (job is a local variable from iteration in office jobs)

= link_to edit_job_path (I18n.locale, job)

-> localhost: 3000 / de / jobs / 10 / edit

When I click on the edit link, I go to the edit page. So far, so good, but the form looks like this:

 <form accept-charset="UTF-8" action="/10/jobs/10" class="edit_job" enctype="multipart/form-data" id="edit_job_10" method="post"> 

Note that my locale (de in this example) has disappeared and instead has a job id!

My routes .rb

 scope "/:locale" do resources :companies resources :offices do resources :jobs end resources :jobs end 

There are two tasks mentioned: perhaps I can do without it, but this is an easy way for me to directly specify the URL of the task to view or add a service variable when creating a new work link and use: office_id (in my control panel controller: link_to new_office_job_path ( I18n.locale, office), then in my form for new jobs: = f.hidden_field: office_id)

But even if I delete the resources: jobs in offices. The locale is still replaced by the job identifier in the edit form.

Please note that I can edit the task correctly, but since the language is changed, the localization text is not right after my redirection.

Any idea how to fix this?

------ Additional requested data -----------

 = form_for(@job) do |f| .field = f.label :name, t(:job_title) = f.text_field :name .field = f.label :url, t(:job_url) = f.text_field :url .field = f.hidden_field :office_id .field = f.label :pdf, t(:job_upload_pdf) = f.file_field :pdf .field = f.label :tag_list, t(:job_tags) = f.text_field :tag_list .actions = f.submit "Submit", :class => "btn btn-primary" 

------ Additional Information -----------

BTW: this work works, I get / de / jobs / 10, but I would like to understand why the locale gets corrupted if I use default formfor.

  = form_for @job, :url => job_path(I18n.locale, @job) do |f| 
+4
source share
2 answers

You can process the locale using the Routing Filter , I tried it in my project, you do not need to worry about the language setting in the routes the filter will process it for you.

 # in config/routes.rb Rails.application.routes.draw do filter :locale end 

I hope this solves your problem.

0
source

The form_for line builds the path; when creating the path, you also need to specify the locale. To do this cleanly (without explicitly specifying a URL, which is also possible), write it as follows:

 = form_for [I18n.locale, @job] do |f| 

and that should do the right way. As stated in the documentation , it will use the array to build the correct path (this works for namespaces and nested routes, so I assume it will work for your language too).

An alternative is to specify the path explicitly using the :url option.

NTN.

0
source

Source: https://habr.com/ru/post/1469264/


All Articles