I have a typical Rails model form with a file attachment selector allowing multiple attachments. It works fine in development, but during a system test, raises an ActiveSupport::MessageVerifier::InvalidSignature exception.
Rails 7.0.2.2
capybara 3.36.0
rack-test 1.1.0
The model has_many_attached :photos.
The form is using form_with and multipart: true.
The HTML source looks correct.
In development, manually using the form with 0 or any file attachments works as expected.
In my system test, I am using the rack_test driver.
test "creating a quote request" do
visit new_quote_request_path
fill_in "First name", with: 'FAKE FIRST'
# ...
click_on "Submit"
assert_text "Success"
end
In the controller, my canonical param-permitting method looks like:
def quote_request_params
params.require(:quote_request).permit(:first_name, :last_name, :email,
:phone_number, :shipping, :promo_code, :description, :item_type_id, :brand_id,
photos: [])
end
My controller create method is typical...
def create
#quote_request = QuoteRequest.new(quote_request_params)
respond_to do |format|
# ...
In the system test, the call of QuoteRequest.new(quote_request_params) raises an ActiveSupport::MessageVerifier::InvalidSignature exception.
With a breakpoint in place, I can see that the quote_request_params looks like:
#<ActionController::Parameters {"first_name"=>"FAKE FIRST",
"last_name"=>"FAKE LAST", "email"=>"fake#fake.com",
"phone_number"=>"5415555555", "shipping"=>"1", "promo_code"=>"",
"description"=>"Fake quote request description.",
"item_type_id"=>"980190962", "brand_id"=>"980190962",
"photos"=>[
"",
"#<Capybara::RackTest::Form::NilUploadedFile:0x000000010dae35b8>"
]} permitted: true>
And it seems Capybara is doing its default behavior of attaching a 'nil file' for the multipart form.
Why is the test raising an ActiveSupport::MessageVerifier::InvalidSignature exception?
This is an issue with Rails 7 and rack-test. It can temporarily be solved by putting the following in config/environments/test.rb:
config.active_storage.multiple_file_field_include_hidden = false
Refer to the rack-test issue for more details.
Observe the following console output:
$ grep -IirF admin config/locales/
config/locales/static.el.yml: Administered: Χορηγήθηκε
config/locales/el.yml: admin: διαχειριστής
config/locales/system.el.yml: administered: Χορηγήθηκε
$ rails c
Running via Spring preloader in process 8176
Loading development environment (Rails 6.0.2.1)
irb(main):001:0> helper.t('admin1', locale: :el)
=> "admin1"
irb(main):002:0> helper.t('admin', locale: :el)
=> "διαχειριστής"
irb(main):003:0> helper.t('admin1', locale: :en)
=> "admin1"
irb(main):004:0> helper.t('admin', locale: :en)
=> {:js=>{true=>true, false=>false, :is_present=>"Is present", :is_blank=>"Is blank", :date=>"Date ...", :between_and_=>"Between ... and ...", :today=>"Today", :yesterday=>"Yesterday", :this_week=>"This week", :last_week=>"Last week", :number=>"Number ...", :contains=>"Contains", :is_exactly=>"Is exactly", :starts_with=>"Starts with", :ends_with=>"Ends with", :too_many_objects=>"Too many objects, use search box above", :no_objects=>"No objects found"}, :loading=>"Loading...", :toggle_navigation=>"Toggle navigation", :home=>{:name=>"Home"}, :pagination=>{:previous=>"« Prev", :next=>"Next »", :truncate=>"…"}, :misc=>{:search=>"Search", :filter=>"Filter", :refresh=>"Refresh", :show_all=>"Show all", :add_filter=>"Add filter", :bulk_menu_title=>"Selected items", :remove=>"Remove", :add_new=>"Add new", :chosen=>"Chosen %{name}", :chose_all=>"Choose all", :clear_all=>"Clear all", :up=>"Up", :down=>"Down", :navigation=>"Navigation", :root_navigation=>"Actions", :navigation_static_label=>"Links", :log_out=>"Log out", :time_ago=>"%{time} ago", :ago=>"ago", :more=>"Plus %{count} more %{models_name}"}, :flash=>{:successful=>"%{name} successfully %{action}", :error=>"%{name} failed to be %{action}", :noaction=>"No actions were taken", :model_not_found=>"Model '%{model}' could not be found", :object_not_found=>"%{model} with id '%{id}' could not be found"}, :table_headers=>{:model_name=>"Model name", :last_created=>"Last created", :records=>"Records", :username=>"User", :changes=>"Changes", :created_at=>"Date/Time", :item=>"Item", :message=>"Message"}, :actions=>{:dashboard=>{:title=>"Site Administration", :menu=>"Dashboard", :breadcrumb=>"Dashboard"}, :index=>{:title=>"List of %{model_label_plural}", :menu=>"List", :breadcrumb=>"%{model_label_plural}"}, :show=>{:title=>"Details for %{model_label} '%{object_label}'", :menu=>"Show", :breadcrumb=>"%{object_label}"}, :show_in_app=>{:menu=>"Show in app"}, :new=>{:title=>"New %{model_label}", :menu=>"Add new", :breadcrumb=>"New", :link=>"Add a new %{model_label}", :done=>"created"}, :edit=>{:title=>"Edit %{model_label} '%{object_label}'", :menu=>"Edit", :breadcrumb=>"Edit", :link=>"Edit this %{model_label}", :done=>"updated"}, :delete=>{:title=>"Delete %{model_label} '%{object_label}'", :menu=>"Delete", :breadcrumb=>"Delete", :link=>"Delete '%{object_label}'", :done=>"deleted"}, :bulk_delete=>{:title=>"Delete %{model_label_plural}", :menu=>"Multiple delete", :breadcrumb=>"Multiple delete", :bulk_link=>"Delete selected %{model_label_plural}"}, :export=>{:title=>"Export %{model_label_plural}", :menu=>"Export", :breadcrumb=>"Export", :link=>"Export found %{model_label_plural}", :bulk_link=>"Export selected %{model_label_plural}", :done=>"exported"}, :history_index=>{:title=>"History for %{model_label_plural}", :menu=>"History", :breadcrumb=>"History"}, :history_show=>{:title=>"History for %{model_label} '%{object_label}'", :menu=>"History", :breadcrumb=>"History"}}, :form=>{:cancel=>"Cancel", :basic_info=>"Basic info", :required=>"Required", :optional=>"Optional", :one_char=>"character", :char_length_up_to=>"length up to", :char_length_of=>"length of", :save=>"Save", :save_and_add_another=>"Save and add another", :save_and_edit=>"Save and edit", :all_of_the_following_related_items_will_be_deleted=>"? The following related items may be deleted or orphaned:", :are_you_sure_you_want_to_delete_the_object=>"Are you sure you want to delete this %{model_name}", :confirmation=>"Yes, I'm sure", :bulk_delete=>"The following objects will be deleted, which may delete or orphan some of their related dependencies:", :new_model=>"%{name} (new)"}, :export=>{:confirmation=>"Export to %{name}", :select=>"Select fields to export", :select_all_fields=>"Select All Fields", :fields_from=>"Fields from %{name}", :fields_from_associated=>"Fields from associated %{name}", :display=>"Display %{name}: %{type}", :options_for=>"Options for %{name}", :empty_value_for_associated_objects=>"<empty>", :click_to_reverse_selection=>"Click to reverse selection", :csv=>{:header_for_root_methods=>"%{name}", :header_for_association_methods=>"%{name} [%{association}]", :encoding_to=>"Encode to", :encoding_to_help=>"Choose output encoding. Leave empty to let current input encoding untouched: (%{name})", :skip_header=>"No header", :skip_header_help=>"Do not output a header (no fields description)", :default_col_sep=>",", :col_sep=>"Column separator", :col_sep_help=>"Leave blank for default ('%{value}')"}}, :history_rollback=>{:cancel=>"Cancel", :confirm=>"Are you sure?", :table_headers=>{:changes=>"Changes", :field=>"Field", :rollback=>"Rollback", :view_changes=>"View changes"}, :rollback_destroy=>"Rolled back newly-created record by destroying it.", :rollback_success=>"Rolled back changes to this record.", :rollback_impossible=>"Couldn't rollback. Sorry.", :version_not_found=>"Version does not exist.", :rollback_violates_foreign_key=>"Couldn't rollback: this model links to another model, which doesn't exists."}}
Translation seems to work correctly, except for a single word: 'admin'
Can someone explain this error, and how to overcome it?
This most likely has to do with a conflict with some other gem that adds an admin namespace, like maybe ActiveAdmin or rails_admin, or maybe a role authorization plugin. That's why it's returning a hash instead of a single string value.
Some other code or gem has loaded the admin key to look something like this:
en:
admin:
js:
'true': true
'false': false
is_present: Is present
....etc....
I would suggest nesting your key to avoid the conflict with something like:
en:
user_type:
admin: Administrator
helper.t('user_type.admin')
I would also suggest setting up the i18n-tasks gem, which can find and correct issues with translation files, as part of your CI or test suite, which I believe would have detected this conflict with the command i18n-tasks health and shown that admin is a dupilcate key, so when your yaml files and the gem's get combined, only the last one to write to the translations hash will be preserved.
Per the Rails 3.2 API Docs, to use different locales for number_to_currency, I need to do the following:
<%= number_to_currency(1234567890.506, :locale => :fr) %>
I was expecting the following output:
# => 1 234 567 890,51 €
Even though I literally use that exact thing within my app and it keeps outputting the following:
$1,234,567,890.51
When I check for the available_locales within my app I get the following:
> I18n.available_locales
=> [:en, :de, :es, :fr, :ja, :pl, :"pt-BR", :ru, :sv, :"zh-CN"]
So it SHOULD work, but it doesn't.
What am I missing?
Update 1
Per #s3tjan's comment, I did some digging in that linked Rails issue and that led me to my application.rb where I discovered I18n.enforce_available_locales = false. I changed that to true and restarted the server.
When I tried the above again, I am now getting this error:
ActionView::Template::Error (:fr is not a valid locale):
Not sure how to fix this.
Update 2
So I just realize that I never had a locale file in my config/locales. What I really want is to use the GBP Pounds for currency, so I added an en-GB.yml file in my config/locales, then I restarted my server and console.
In my application.rb, I have the following:
I18n.enforce_available_locales = true
Then I checked my console and got this:
[1] pry(main)> I18n.available_locales
=> [:en, :de, :es, :fr, :ja, :pl, :"pt-BR", :ru, :sv, :"zh-CN", :"en-GB"]
[2] pry(main)>
So the :"en-GB" was added successfully to my app's load path.
But when I do this in my view:
<%= number_to_currency(1234567890.506, :locale => :"en-GB") %>
This is the error I get:
:"en-GB" is not a valid locale excluded from capture due to environment or should_capture callback
ActionView::Template::Error (:"en-GB" is not a valid locale):
So still not working.
Update 3
My en-GB.yml file was taken directly from https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en-GB.yml
So it looks exactly like that. Yet I am still getting the same error:
ActionView::Template::Error (:"en-GB" is not a valid locale):
Synopsis:
Remove custom language ymls and add the correct version of the i18n-rails gem. This resolved this special issue.
Original answer:
Ok my guess is that your en-GB.yml is empty. So it actually finds the file and adds the locale in I18n.available_locales BUT this does not include that all translations are available.
When you look at the format of such a yml file you will recognize they all start with
---
language-code
some_keys: ...
This is what actually is loaded into memory and therefore provides all the available translations. Available locale is just defined by found files in config/locales.
When you check the source of number_to_currency It takes the locale from the options and passes it along the key it looks for to I18n.
I18n.translate(:'number.format', :locale => options[:locale], :default => {})
Since you just say that en-GB is available but don't have the actual keys along the locale in memory you get the missing translation issue.
What I suggest is you either use the content of the linked yml file and paste it into your en-GB.yml or you remove your en-GB.yml and find a 3.2 working i18n-rails version and use it. i18n-rails provides plenty of default translations which are utilized all over default rails.
Addition:
Before you added the en-GB.yml file it actually worked like expected.
When no locale is found it defaults to dollar in here since the currency variable will be just and empty {}.
Somehow, I found the solution that worked for me.
First of all, you need to have a locale file with your requirements in it.
Here is the example of fr.yml file
For an instance, copy and paste this file in app/config/locales/
then restart your console,
then try, number_to_currency(1000.51, locale: :fr)
for sure, you will get '1 000,51 €'
Here is the full list of all supported countries' locale file.
Until and unless you don't have locale file with your format required in it, you won't get the desired result.
my rails version is 3.2.22.5, ruby is 2.4.2(I didnt install below 2.x.x)
I use locale yml from
https://github.com/svenfuchs/rails-i18n/blob/rails-3-x/rails/locale/en-GB.yml
This is work well
<%= number_to_currency(1234567890.506, :locale => :"en-GB") %>
<%= number_to_currency(1234567890.506, :locale => "en-GB") %>
to result
£1,234,567,890.51
and I add fr.yml too like
fr:
...
number:
currency:
format:
...
unit: €
And then this is work too
<%= number_to_currency(1234567890.506, :locale => :fr) %>
to result
€1,234,567,890.51
I didn't change or add configuration. I add only controller, view and locale file. And test it.
You could translate directly like, so test this
I18n.translate(:'number.currency.format', :locale => "en-GB", :default => {})
if it occur a same error, then check out you file's name, extension, path.
and you must restart server
I have a ruby on rails application. I have spanish and english support in my application. However, I get a translation missing exception in case spanish mode.
I have the following model:
class Company < ApplicationRecord
validates :name, length: { in: 5..15, message: :bad_name }
end
en.yml
en:
activerecord:
errors:
models:
company:
attributes:
name:
bad_name: 'message in english'
sp.yml
sp:
activerecord:
errors:
models:
company:
attributes:
name:
bad_name: 'message in spanish'
Just in case the error when I open the application in english. I get the "message in english" message and that's ok.
On the other hand, when I open and test it in spanish, I get the following error.
ActiveRecord::RecordInvalid:
translation missing: sp.activerecord.errors.messages.record_invalid
I can't see what I'm missing,
Any suggestions,
Thanks.
Are other translations work in this file?
Try to rename sp.yml to es.yml, because this is the iso-code for "espanol".
Also try to include a message for activerecord.errors.messages.record_invalid in your spansih file. will this be displayed?
Actually, to use Spanish within your locale you would need to set I18n.locale = :es. I think you mixed up your locales. The correct language file should be named es.yml and look like:
es:
activerecord:
errors:
models:
company:
attributes:
name:
bad_name: 'message in spanish'
So the issue I've encountered days ago still makes me unconfortable with my code. I can't get proper translation for my application: Yml looks like that:
pl:
errors: &errors
format: ! '%{attribute} %{message}'
messages:
confirmation: nie zgadza się z potwierdzeniem
activemodel:
errors:
<<: *errors
activerecord:
errors:
<<: *errors
And model looks like that:
module Account
class User < ActiveRecord::Base
attr_accessor: password_confirmation
end
end
And flash in controller declared:
flash[:errors] = #user.errors.full_messages
I tried and read activerecord documentation and stack previous questions (How to use Rails I18n.t to translate an ActiveRecord attribute?, Translated attributes in Rails error messages (Rails 2.3.2, I18N)). Yet it still doesn't work as I wanted.
password_confirmation remains "Password Confirmation", and not "Potwierdzenie hasła" as it shoul be. The screenshot might explain it better: http://i42.tinypic.com/1glz5.png
Your User model is in a namespace, thus you have to declare the namespace in your translation file also. Try the following to get the correct translation for password_confirmation:
pl:
activerecord:
attributes:
account/user:
password_confirmation: "Potwierdzenie hasła"