Rails 6 internationalization - what has changed? - ruby-on-rails

I started a fresh new Rails 6 project and got stuck to figure out why what obviously just worked (it's not the first app I start), fails...
So I created a dummy simply rails app with no additional gems and a home#index page:
<h1>Home#index</h1>
<p>
<%= t('hello.world') %>
</p>
Then I added a translation for the above key to config/locales/en.yml:
en:
hello: "Hello !"
world: "Hello World!"
and respected 1 tab indentation.
When navigating to localhost:3000/home/index I got the weird error:
/Users/serguei/.rvm/gems/ruby-2.7.0/gems/i18n-1.8.2/lib/i18n.rb:195: warning: The called method `translate' is defined here
Rendered home/index.html.erb within layouts/application (Duration: 6.2ms | Allocations: 3150)
Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.0ms | Allocations: 5045)
ActionView::Template::Error (can not load translations from /Users/serguei/projects/rails/draft-app/config/locales/en.yml: #<Psych::SyntaxError: (<unknown>): did not find expected key while parsing a block mapping at line 2 column 3>):
1: <h1>Home#index</h1>
2: <p>
3: <%= t('hello.world') %>
4: </p>
app/views/home/index.html.erb:3
When changed the called translation to just hello:
<h1>Home#index</h1>
<p>
<%= t('hello') %>
</p>
and removing the last line from the en.yml file:
en:
hello: "Hello !"
it works.
Why so? What has changed since Rails 5 there? Can't we use nested translations anymore in locales files? Rails guides have nothing special about that. Or am I missing something?
Adding rails-i18n gem to the Gemfile didn't solve the problem.
Rails version: 6.0.2.1
Ruby version: ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin19]

If you want to nest it, then you can't assign a string value to the parent, do this instead
en:
hello:
world: "Hello World!"
Then, in erb, this will work
<%= t('hello.world') %>
Give it a try.

Related

redmine upgrade to 3.4.4 not working [ActionView::Template::Error (undefined method `default_users_time_zone')]

Every thing worked fine for me with Redmine 3.3. Now I wanted to upgrade to 3.4.4 and Redmine is no longer running. I searched the internet but did not find the exact same problem. Similar problems where caused by plugins but I do not have any plugins installed. I use the Passenger Docker image of Redmine.
The logs give me the following error message:
Processing by WelcomeController#index as HTML
Current user: anonymous
Redirected to http://0.0.0.0/login?back_url=http%3A%2F%2F0.0.0.0%2F
Filter chain halted as :check_if_login_required rendered or redirected
Completed 302 Found in 60ms (ActiveRecord: 3.7ms)
App 179 stdout:
Started GET "/login?back_url=https%3A%2F%2Fredmine.mydomain.tld%2F" for 95.90.204.176 at 2018-03-14 19:10:26 +0000
Processing by AccountController#login as HTML
Parameters: {"back_url"=>"https://redmine.mydomain.tld/"}
Current user: anonymous
Rendered account/login.html.erb within layouts/base (24.1ms)
Completed 500 Internal Server Error in 219ms (ActiveRecord: 4.3ms)
ActionView::Template::Error (undefined method `default_users_time_zone' for #<Class:0x00000000055f2120>
Did you mean? default_users_hide_mail):
11: <%= favicon %>
12: <%= stylesheet_link_tag 'jquery/jquery-ui-1.11.0', 'application', 'responsive', :media => 'all' %>
13: <%= stylesheet_link_tag 'rtl', :media => 'all' if l(:direction) == 'rtl' %>
14: <%= javascript_heads %>
15: <%= heads_for_theme %>
16: <%= call_hook :view_layouts_base_html_head %>
17: <!-- page specific tags -->
app/models/user_preference.rb:44:in `initialize'
app/models/user.rb:949:in `pref'
app/helpers/application_helper.rb:1451:in `javascript_heads'
app/views/layouts/base.html.erb:14:in `_app_views_layouts_base_html_erb__2446195152155203147_36658300'
lib/redmine/sudo_mode.rb:63:in `sudo_mode'
What could that be? I need to upgrade to 3.4 because I want to use a plugin but the plugin requires version 3.4 in use.
It was my fault! I did not copy every new settings file to my mounted config directory.

Impossible to delete a flash

I made a mistake before migrating a plugin, and have written
flash[:notice] = :label_presta_added
instead of
flash[:notice] = l(:label_presta_added)
I corrected my mistake but it seems that my Redmine Plugin has trashed my Redmine. Even though I delete my plugin a migrate once again, I still get this error:
Started GET "/" for 127.0.0.1 at 2016-06-01 22:21:37 +0200
Processing by WelcomeController#index as HTML
Current user: admin (id=1)
Rendered welcome/index.html.erb within layouts/base (28.1ms)
Completed 500 Internal Server Error in 366ms (ActiveRecord: 116.0ms)
ActionView::Template::Error (undefined method `html_safe' for :label_presta_added:Symbol
Did you mean? html_safe?):
97: <div id="sidebar">
98: <%= yield :sidebar %>
99: <%= view_layouts_base_sidebar_hook_response %>
100: </div>
101:
102: <div id="content">
103: <%= render_flash_messages %>
app/helpers/application_helper.rb:312:in `block in render_flash_messages'
app/helpers/application_helper.rb:311:in `render_flash_messages'
app/views/layouts/base.html.erb:100:in `_app_views_layouts_base_html_erb__4104276684161420982_39604440'
lib/redmine/sudo_mode.rb:63:in `sudo_mode'
Can somebody give me a hand here?
Thanks in advance!
This is stored in your session, so usually changing the session secret key will invalidate all sessions and discard any old session data.
You can also try and rescue to clear it out as a one-time deal.
Have you restarted the server? Or you can use flash[:notice] = nil to remove it.
It looks like it throws a html_safe error. Can you see if the method which is rendering the flash is using html_safe? It looks like its coming from there.
Not sure exactly, may be shooting in the dark.
But read these and try may be:
actionview::template::error(undefined method 'html_safe' for nil:NilClass)
http://www.redmine.org/issues/8477

Using erb in database.yml breaks Devise

I updated by database.yml to get credentials out of the file for security purposes, and replaced them with a call via erb to an object that reads the info from a file.
This seems to work fine when authentication isn't involved, but when devise generated views seem to break.
This version of database.yml that breaks it:
development:
adapter: sqlserver
host: my_server.xxx.rds.amazonaws.com
port: 1433
database: MyDatabase
username: <%= CredentialsManager.get_db_user %>
password: <%= CredentialsManager.get_db_pass %>
But the console, and pages that don't call authentication have no problem accessing model attributes.
However views like devise/sessions/new.html.erb, seem to be looking in the wrong place for the User model. Form breaks with errors like these, where the user model appears to have no 'email' attribute:
Processing by Devise::SessionsController#new as HTML
Rendered devise/sessions/new.html.erb within layouts/application (6.0ms)
Completed 500 Internal Server Error in 18ms
ActionView::Template::Error (undefined method `email' for #<User >):
3: <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
4: <div><%= f.label :email %>
5: <br/>
6: <%= f.email_field :email %></div>
7:
8: <div><%= f.label :password %>
9: <br/>
app/views/devise/sessions/new.html.erb:6:in `block in _app_views_devise_sessions_new_html_erb__882742671_47657856'
app/views/devise/sessions/new.html.erb:3:in `_app_views_devise_sessions_new_html_erb__882742671_47657856'
Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (16.0ms)
Examining the |f| parameter in the debugger shows that it has an #object attribute of class User, but the #attributes hash of #object is an empty hash, and #column_names is an empty array.
Does anyone have an idea how to fix this? I need to get credentials out of database.yml, and this is the way I'd like to do it.
Environment Rails 3.2.11
Devise 2.1.2
Warden 1.2.1
This problem was resolved by changing the db username from 'asadmin_app' to just 'asadmin'
No idea why devise doesn't like db usernames with underscores, but it seems like some kind of bug to me. Rails itsef, and SQL Server (the backing DB) have no problem with _ in a username, but soemthing goes screwy when devise and warden get involve.d

Rails jQuery adapter rendering thrice

I'm following the Beginning Rails 3, Updated book from Apress 2010. The problem I'm having is with loading a Template dynamically with Ajax using the jQuery adapter. Everything works but it appears to be rendering three times on the page.
Here is how I dynamically load it when the user clicks the "new comment" link.
views/articles/show.html.erb
<%= link_to "new comment",
new_article_comment_path(#article, :format => :js),
:remote => true,
:id => 'new_comment_link' %>
Then I render it as such.
views/comments/new.js.erb
$("<%= escape_javascript render :file => 'comments/new'," +
" :formats => [:html], :handlers => [:erb] %>")
.insertAfter('#comments');
Then I see this in the log.
Started GET "/articles/1/comments/new.js" for 127.0.0.1 at 2013-01-18 14:51:05 -0600
Processing by CommentsController#new as JS
Parameters: {"article_id"=>"1"}
Article Load (0.2ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT 1 [["id", "1"]]
Rendered comments/new.html.erb (53.8ms)
Rendered comments/new.js.erb (54.6ms)
Completed 200 OK in 57ms (Views: 55.9ms | ActiveRecord: 0.2ms)
Notice it renders my erb and the js file? Somehow that ends up with showing up three times on my page.
Any clues on how to fix this? I'm using Rails 3.2.9, rails.js (latest), and jquery-1.9.0.
Thanks!
Solved it!
Turns out I was adding rails.js and jquery.js TWICE!
Here is the skinny: assets/javascripts/application.js is crucial for including javascript files into your app. Basically whatever gets defined there gets pushed out to the page. You simply need to make sure that it gets defined at least once in the app as such.
views/layouts/application.html.erb
<%= javascript_include_tag "application" %>
And that's it! The Ajax jQuery adapter should just work. Becareful not to add any additional files to the javascript folder as those will get pushed out as well and that's exactly what you don't want. Basically I was defining the adapter both through application.html.erb and manually by downloading both files. Hopefully this will help a lost poor soul somewhere along the way.
Happy Hacking.

My route returns a blank view (no html when I do view source)

I am brand new to Ruby on Rails and I have been trying to get a simple default route set up and working. When I try to run my application I get a blank result (if I do a view source, there is nothing there).
Here are the relevant files (not sure if I am missing something that would be useful).
app/config/routes.rb
Blog::Application.routes.draw do
root :to => "home#index"
end
app/views/home/index.html.erb
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
end
end
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Benji</title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
When I try to run my application, I go to http://localhost:3000 and nothing shows up. If I do a view source, it is empty.
If do rake routes this is the result:
JESSE-GAVINs-MacBook-Pro-17:benji jesse$ rake routes
(in /Users/jesse/Dev/benji)
root /(.:format) {:action=>"index", :controller=>"home"}
JESSE-GAVINs-MacBook-Pro-17:benji jesse$
In my development.log file I see this:
Started GET "/" for 127.0.0.1 at Tue Sep 07 10:44:10 -0500 2010
Processing by HomeController#index as HTML
Rendered home/index.html.erb within layouts/application (2.8ms)
Completed 200 OK in 15ms (Views: 14.7ms | ActiveRecord: 0.0ms)
What could be the issue? How do I go about solving this?
Solved (sort of...)
It works just fine when I started the rails server on a different port.
It was my understanding that I didn't need to restart the server in order for it to detect changes in the code.
Sorry about the waste of time, I just didn't know which tree I needed to bark up. I learned some things along the way.
As you're playing with Routes, you absolutely need to restart the server. I don't know if this is part of the problem, but most (all?) things under config/ need the server to be restarted to take effect, particularly routes and environment

Resources