Smartquotes displayed fine in Rails 2, now a problem in Rails 3 - ruby-on-rails

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/

Related

How to use Rails with Mongoid and not PostgreSQL or SQLite?

Added Mongoid but got:
LoadError:
Error loading the 'sqlite3' Active Record adapter.
Removed database.yml and got:
Could not load database configuration. No such file - ["config/database.yml"]
Saved empty database.yml and got:
ActiveRecord::AdapterNotSpecified:
'test' database is not configured. Available: []
Rails 5.2.2
If it is fresh install use
rails new app --skip-active-record
if you already created an App with rails, which has active-record(mysql or sqlite3)
use this link:
Rails with Mongoid
Also remove
active records
lines from
application.rb
ActiveRecord is a library that works only with relational databases. If you want to use MongoDB with mongoid, you don't need ActiveRecord. Remove any mention of AR and relational DBs from the application code, config and Gemfile. If you're stating from scratch, use rails new myapp --skip-active-record
See also: https://gorails.com/guides/setting-up-rails-4-with-mongodb-and-mongoid (related to Rails 4, but should help).

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

Rails 2.3.2 trying to render ERB instead of HAML

Rails is suddenly trying to render ERB instead of Haml and I can't figure out why. I've created new rails projects, reinstalled Haml, and reinstalled Rails.
Here's exactly the steps I take when making my application (Rails 2.3.2):
rails> rails test
rails> cd test
rails\test> haml --rails .
rails\test> ruby script\generate model user email:string password:string
rails\test> ruby script\generate controller users index
rails\test> rake db:migrate
Here's what the UsersController looks like:
class UsersController < ApplicationController
def index
#users = User.all
end
end
My routes:
ActionController::Routing::Routes.draw do |map|
map.resources :users
end
I now create views\users\index.html.haml:
%table
%th(style="text-align: left;")
%h1 Users
- for user in #users
%tr
%td= user.email
%td= user.password
Annnd run the server...
I navigate to localhost:3000\users and I get this error message:
Template is missing
Missing template users/index.erb in view path app/views
For some reason Rails is trying to find and render .erb files instead of .haml files.
vendor\plugins\haml\init.rb exists, untouched.
I've reinstalled Haml (Pretty Penny) multiple times and still get the same results.
I've also tried adding config.gem 'haml' to my environment.rb but this also doesn't work.
I can't figure out why suddenly rails will not render haml for me.
Hi it seem like haml is not enabled as Rails plugin ,in order to enable it use the following command .
Go to your application folder from the command prompt type the following
$ cd ..
$ haml --rails <yourproject>
if this doesnot work try installing haml gem with the following code
$ gem install haml
I tried with above example , it did work for me ,i have haml gem installed in my ubuntu system .
Good luck !
NOTE: "haml --rails" was deprecated in HAML 3.1
It's worth noting that the fact that the error message says that it couldn't find index.erb doesn't mean that it didn't look for index.haml too. The erb extension is hard-coded into the error message.
I thought that I had the same problem you describe, but it turned out that my application simply couldn't find my partial at all - it had nothing to do with the file extension.
I had this same problem (see this post) with Rails 2.3.4. Multiple gem uninstall/gem install rails didn't fix the problem. But downgrading to Rails 2.3.2 worked! (I know HAML previously worked in this project with this version of Rails).
sudo gem install -v 2.3.2 rails
Using Rails 3.1, I ran in to the same error and had to restart the web server.
I have this old project in rails 2.3.18, where the gems are managed using bundler, and all I had to do was explicitly use version 3.1.3 It did not work with the latest version (4.0). So in my version I added
gem 'haml', '3.1.3'
did bundle install and restarted my development server :)
Hmmm strange, this might be related.
According to: http://www.ruby-forum.com/topic/101346 you should use resource_url helpers in controllers and resource_path helpers in views. Right?
BUT, if I do use a resource_url helper in a redirect_to call inside my controller, then I get:
Missing template htp://localhost:4000/categories/new.erb in view path app/views
If I use the resource_path helper instead, there aren't any problems at all.
Anyone knows what could be wrong?
Why is the resource_url helper trying to redirect to an .erb file?
This is the error from the server log:
ActionView::MissingTemplate (Missing template http://localhost:4000/categories/new.erb in view path app/views):
haml (2.2.2) lib/haml/helpers/action_view_mods.rb:13:in `render'
app/controllers/categories_controller.rb:15:in `create'
haml (2.2.2) rails/./lib/sass/plugin/rails.rb:19:in `process'
P.S. This is in Rails 2.3.3
maybe your file name is wrong, if you have a whitespace in the end of the index.html.haml_, rails will wrong...
I ran into the same problem and I had to restart my server after installing Haml before my rails app recognized the changes.
I was having this issue with Ruby 1.9x, Rails 2.3.5, and HAML 3.1. I believe part of the issue is that some of the deprecated calls in 1.8 were removed in 1.9.
IMHO, if you want to use HAML in Rails 2, you'd be better off downgrading to Ruby 1.8. (which is what I did to fix my problem). In Rails 2, you MUST have the gem.config "haml" in your config.
Even better, move forward to Rails 3 on Ruby 1.9!
I've had this same problem.
The solution is documented in https://github.com/haml/haml/issues/672
You need to add
config.after_initialize do
require 'haml'
Haml.init_rails(binding)
end
inside your config/environment.rb within the Rails::Initializer.run do |config| configuration block !!
Updated - Not actual anymore:
I name all of my haml file only .haml
To illustrate:
test.haml
# not
test.html.haml
Update 5 years later:
I recommend to name them "file.format.haml", because its much more clear which format is the outcome...

Resources