Set language in Devise - ruby-on-rails

I am a noob in Ruby/Rails and I am creating my first project now.
I am using Devise gem for authentication system. I have installed it and I am on fight to change the default messages from "en" (default language) to "pt-BR".
I have a file called devise.pt-BR.yml inside /config/locales/ with all translations for this language and I have followed a few tips but when I restart the server, I still get "en" as the default language.
I don't want to have two or more languages, I just need to work with "pt-BR" instead "en".
My environment:
Fedora 16
Ruby 1.9.2p320
Rails 3.2.6
Devise 2.1.2
/config/application.rb content (look at bottom):
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Foo
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Enable escaping HTML in JSON.
config.active_support.escape_html_entities_in_json = true
# Use SQL instead of Active Record's schema dumper when creating the database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql
# Enforce whitelist mode for mass assignment.
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
config.active_record.whitelist_attributes = true
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.i18n.load_path += Dir[Rails.root.join('devise', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :"pt-BR"
I18n.locale = :"pt-BR"
end
end

Change the line
config.i18n.default_locale = :"pt-BR"
to
config.i18n.default_locale = "pt-BR"

I was wrong, it's working!
I had to drop it inside application.rb:
config.i18n.load_path += Dir[Rails.root.join('locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :'pt-BR'
And put "devise.pt-BR.yml" inside /config/locales/
I was looking for "Sign in" and label from fields at "Log in" view but I think these strings are not translatable, I will generate these views and manually revise them.
Thank you.

Try renaming your locale file to pt-BR.yml, or else change your reference in application.rb to devise.pt-BR.yml.

Related

Rails custom configuration returns an empty hash

I'm using Rails 4, and I would like to use the custom configuration functionality as explained here:
http://guides.rubyonrails.org/configuring.html#custom-configuration
I created the following YAML file (config\prefs.yml):
development:
password: test
And I added this to my config/application.rb:
module MyApp
class Application < Rails::Application
# ...
config.x.prefs = Rails.application.config_for(:prefs)
end
end
When I go to the rails console, I get this:
> Rails.configuration.x.prefs
=> {}
Why isn't Rails correctly loading the configuration?
I'm guessing the following:
You have the Spring gem bundled in.
Your custom configuration somehow got initialized in the state it is currently in. (i.e. empty)
The config\prefs.yml isn't tracked by Spring, so it doesn't know the environment needs to be reloaded.
If i'm correct, you'll just have to create an initializer with the following code:
Spring.watch "config/prefs.yml"
And, of course, you'll have to reload the console each time the config is changed. I've managed to reproduce and solve your issue with this, so i hope this helps.
I try your code on my machine and working fine i think maybe problem with configuration you did in config/application.rb or you need to reload your rails console with reload command reload!
that my configuration for config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module WSApp
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
config.x.prefs = Rails.application.config_for(:prefs)
end
end
that your file prefs.yml
development:
password: test
here is the result

application.rb paths.app.views undefined method `app' for Rails::Paths::Root

using devise 2.1.0
I had to override the devise registration controller. In the process I had to move my devise views directory to correspond with the new controller. I have this code in my config/applications.rb file
paths.app.views << "app/views/devise"
And it is throwing an error when I try and start my server with rails s:
...config/application.rb:65:in `<class:Application>': undefined method `app' for #<Rails::Paths::Root:0x00000103537740> (NoMethodError)
I am relatively new at rails. But I get that there is a root class somewhere that didn't define app. I got this advice to use this paths.app.views here at stack.
Here is the full applications.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Growle
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password, :password_confirmation]
#don't generate RSpec tests for views and helper
config.generators do |g|
g.view_specs false
g.helper_specs false
end
# Use SQL instead of Active Record's schema dumper when creating the database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql
# Enforce whitelist mode for mass assignment.
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
config.active_record.whitelist_attributes = true
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
paths.app.views << "app/views/devise"
end
end
By reading the last comment of yours, it sound that you want either view_paths= or prepend_view_path.
Those can be set at class level in your controller.
If that isn't what you are looking for, consider reading Override devise registrations controller for information.

Undefined method `groups' error when running generate scaffold command

I am absolutely new to Ruby and Rails and I am getting an error when running this command:
rails generate scaffold User name:string email:string
Here is the full error I am getting:
/Users/ad7863/rails_project/demo_app/config/application.rb:7: undefined method `groups' for Rails:Module (NoMethodError)
from /Library/Ruby/Gems/1.8/gems/railties-3.0.11/lib/rails/commands.rb:15:in `require'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.11/lib/rails/commands.rb:15
from script/rails:6:in `require'
from script/rails:6
I am following the Ruby on Rails tutorial which can be found here.
I am using Rails version 3.0.11.
Anyone have any idea what I'm doing wrong?
Thanks.
Edit: the contents of my application.rb file:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module DemoApp
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
end
end
On line 7 of your application.rb file, change:
Bundler.require(*Rails.groups(:assets => %w(development test)))
to:
# Bundler.require(*Rails.groups(:assets => %w(development test)))

can't modify frozen array (TypeError) - config/application.rb:42:in `<<':

I have a rails 3 app, and am trying to do testing. I do the following command rspec spec/controllers/ and get the following error:
/config/application.rb:42:in `<<': can't modify frozen array (TypeError)
from c:/Users/#####/documents/#####/config/application.rb:42
It is pointing to my config/application.rb file which I have provided below.
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
module PadOnRails
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# JavaScript files you want as :defaults (application.js is always included).
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
config.autoload_paths << "#{config.root}/lib"
end
end
has anyone else experienced this exact kind of problem. If so how do you fix it because I have deleted my project and did a git clone all over and still get the same error.
Try the method the comments suggest:
config.autoload_paths += %W{#{config.root}/lib}
This doesn't modify the initial array, but adds the given array to config.autoload_paths and saves it into a new array in config.autoload_paths.

Rake task on rails 3 - config not found

Releated to this: Rake on Rails 3 problem
I'm trying to copy a rake task working under rails 2, into a rails 3 app.
The rake task is the following:
namespace :cached_assets do
desc "Regenerate aggregate/cached files"
task :regenerate => :environment do
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::AssetTagHelper
stylesheet_link_tag :all, :cache => CACHE_CSS_JS
javascript_include_tag "a.js", "b.js",
"c.js", :defaults, :cache => CACHE_CSS_JS
javascript_include_tag "q.js", "w.js",
"e.js", :cache => 'abc'
end
end
On Rails 2 it cache the assets correctly, on rails 3 I have the following error:
rake cached_assets:regenerate --trace
(in /var/www/apps/****)
** Invoke cached_assets:regenerate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute cached_assets:regenerate
rake aborted!
undefined local variable or method `config' for #<Object:0xa86290>
/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_view/helpers/asset_tag_helper.rb:498:in `stylesheet_link_tag'
/var/www/apps/****/lib/tasks/cached_assets.rake:10
/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
Looking at the definition of stylesheet_link_tag I see:
def stylesheet_link_tag(*sources)
options = sources.extract_options!.stringify_keys
concat = options.delete("concat")
cache = concat || options.delete("cache")
recursive = options.delete("recursive")
if concat || (config.perform_caching && cache)
joined_stylesheet_name = (cache == true ? "all" : cache) + ".css"
joined_stylesheet_path = File.join(joined_stylesheet_name ......
..... ....
My config/application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
module ****
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# JavaScript files you want as :defaults (application.js is always included).
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
config.time_zone = 'UTC'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de
# use memcache
config.cache_store = :mem_cache_store, 'localhost:11211', { :namespace => 'nar_' }
config.after_initialize do
require 'lib/core_extensions.rb'
end
end
end
My config/environments/development.rb:
****::Application.configure do
# Settings specified here will take precedence over those in config/environment.rb
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = false
# EMAIL -> see also initializers/emailer_initializers.rb and /config/email.yml !!!!!!!!!!!!
EMAIL_ENABLED = false
config.action_mailer.raise_delivery_errors = true
MAIL_FROM = '****'
RCPT_TO_DEV = '****'
ENV['RAILS_ASSET_ID'] = '100'
PRODUCTION_DOMAIN_NAME = "*****"
#CSS CACHING
CACHE_CSS_JS = false
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
end
I had the same problem and found a solution here: http://railsforum.com/viewtopic.php?id=42282
Just prefix
stylesheet_link_tag
and
javascript_include_tag
with
ApplicationController.helpers.
then you shouldn't even have to include the ActionView helpers.

Resources