Add "# coding: utf-8" to all files - ruby-on-rails

Can I somehow configure Rails to avoid the need to add # coding: utf-8 to all files? Or must I add it to each file manually?
UPD
To improve my life I have found this gem:
magic_encoding
It won't fix the problem, but it will add magick line to each file. Related topic: Why are all strings ASCII-8BIT after I upgraded to Rails 3?

In a rails application you can specify the default chracter encoding in your application config.
Add the following code inside the Application class in in config/application.rb
Looks something like:
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"

Related

Ruby on Rails 4.2.0: How to find the directories set for loading libraries etc

Is there a environment variable and some command-line options to check my rails application's directories that are set automatically or configured somewhere so that rails know where to load libraries etc.?
As a compare, ruby has a command line option (-I):
ruby -I path:another/path:/usr/lib rubyfile.rb arg1 arg2
such that users can set the directories where libs can be looked up by Ruby.
Similarly, here I am asking if for rails there is a similar way to check the loading paths for Rails applications? Or if there is certain variable set somewhere?
Highlight: I am using Rails version 4.2.0. The config/application.rb sample code doesn't appear to contain config.autoload_paths, not even in comments, which used to be the one for Rails 3 and Rails 4.1.x.
And, how would the Rails application know where to look up for Gems, etc.?
Thanks!
See this file:
config/application.rb
Specifically, you add this setting:
config.autoload_paths
You can add to the autoload paths like this:
config.autoload_paths << Rails.root.join('mydir')
The config.autoload_paths accepts an array of paths from which Rails will autoload constants.
The default is all directories under ./app, such as controllers, models, etc.
The default does not autoload from the typical top-level ./lib directory. If you want to autoload from lib:
config.autoload_paths << Rails.root.join('lib')
See the Rails Guides for Configuring
Rails knows where to look for gems a variety of ways: typically your environment GEM_PATH or the bundler gem bundle exec command. For example, I personally bundle my Rails gems into ./vendor/bundle/.

rest-open-uri on heroku with ruby 2.0

Since the default encoding is now utf-8 for Ruby 2.0, the rest-open-uri gem throws the following error on heroku:
/app/vendor/bundle/ruby/2.0.0/gems/bundler-1.5.2/lib/bundler/runtime.rb:76:in `require': /app/vendor/bundle/ruby/2.0.0/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb:413: invalid multibyte escape: /[\r\n\t !#-[]-~\x80-\xff]+|(\[\x00-\x7f])/ (SyntaxError)
I need to change the following line in rest-open-uri.rb. How can I access this file on heroku?
lib/ruby/gems/2.0.0/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb
# encoding: US-ASCII
rest-open-uri looks like a dead project. Do you want to get in the business of patching up 8 year old code to get your application to work?
It looks like rf-rest-open-uri is a more up-to-date fork of that project, have you tried it? I don't see any encoding instruction in that gem's rest-open-uri.rb source file.

Configuring Rails to generate the correct first line in development.rb

I am using Rails 4.1.1 and Ruby 2.0.0. When I run rails new test_app, the generated file test_app/config/environments/development.rb always contains the first line:
Rails.application.configure do
The above line causes an undefined local variable error. It should be:
TestApp::Application.configure do
Is there a way to configure rails so that it always generates the correct name?
Using Ruby 2.0.0 and Rails 4.1.1, I just created a new Rails app using the command rails new test_app_rails_4-1-1. The top line of the development.rb file is Rails.application.configure do, which is the correct syntax.
I just looked at a the development.rb file in a Rails 4.0.1 app. The first line in that file is HeyLookARailsApp::Application.configure do. So it seems like the syntax in Rails 4.1.1 is different that Rails 4.0.1.

Ruby on Rails - Fixtures populate sqlite3 database with wrong utf-8 encoding

Seeding the sqlite3 database with Fixtures did work with config1 but with config2 utf-8 support fails.
config1: Ubuntu 11.04, ruby 1.9.2p136, Rails 3.0.3, "development" environment
config2: OS X Lion, ruby 1.9.2p290, Rails 3.0.3, "development" environment
When having a yml file like
001:
id: 1
name: "\xC3\xBC"
I use it to populate the database with
Fixtures.create_fixtures(...)
in seed.rb
Afterwards the database shout have an entry with id 1 and name "ü". That would be correct.
That worked with config1. With config2 I alway have an entry with name "ü". utf-8 encoding seems to fail.
I already turned on UTF-8 everywhere in the rails project:
encoding: utf-8
in database.yml
# encoding: utf-8
in seeds.rb
Encoding.default_external = "UTF-8"
Encoding.default_internal = "UTF-8"
config.encoding = "utf-8"
in config/environments/development.rb
Is it possible that these two systems have different endianness?
From the SQLite3 release notes:
Support for UTF-8 and UTF-16
The new API for SQLite 3.0 contains routines that accept text as both
UTF-8 and UTF-16 in the native byte order of the host machine. Each
database file manages text as either UTF-8, UTF-16BE (big-endian), or
UTF-16LE (little-endian).
You can check here:
http://en.wikipedia.org/wiki/Endianness#Endianness_and_operating_systems_on_architectures

Smartquotes displayed fine in Rails 2, now a problem in Rails 3

My Rails app is working from a mysql database, and when I switched from Rails 2.8, Ruby 1.8 and the mysql gem over to Rails 3.0.7, Ruby 1.9.2 and the mysql2 gem, all of a sudden my pages are rendering with smartquotes (curly quotes) and em-dashes, etc., all looking like gibberish.
I'm assuming this has something to do with UTF-8, but I don't know how to pinpoint it.
Here's what I do know:
(1) config/database.yml has the following:
development:
adapter: mysql2
encoding: utf8
(2) config/application.rb has the following:
config.encoding = "utf-8"
Don't know where to go from there.
Any suggestions?
You can narrow it down to the db vs the view by doing two things:
rails console # in your terminal in the project directory, then test that your ActiveRecord objects have the correct data.
ViewSource in your browser to see if the generated source is putting in the correct characters.
Later:
This is probably a duplicate of: mysql2 gem, Rails 3.0.3 and "incompatible character encodings" errors https://github.com/brianmario/mysql2/issues/124
And maybe even the monkeypatch here: http://www.rorra.com.ar/2010/07/30/rails-3-mysql-and-utf-8/

Resources