Parsing YAML Rails fails with UTF8 problems, IRB does fine? - ruby-on-rails-3

i currently try to import some XML via XMLSimple in my RoR-3 App.
on 1 position, that XML stores a YAML-Structure. Dont slap me, i did not create the xml ;)
now that i have it stored in a variable, lets say yamldata, i want to parse its content.
so i do:
chunks = YAML::load yamldata
and thats pretty it.
when i now echo these chunks-values to console, it get this:
Bülach vert
what i wanted to have is:
Bülach vert
when i use exact same sourcecode in IRB, i actually get this 'ü' instead of 'ü'.
i really dont know what to do here.
my Gemfile:
cat Gemfile
source 'http://rubygems.org'
gem 'rails', '3.1.0'
gem 'mysql2'
gem 'haml'
gem 'activerecord', :require => "active_record"
gem 'xml-simple'
gem 'hpricot'
ruby -v:
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
i`d be happy with any idea.
Note: i echo these values before!! i store them into a model, so i guess that its not a sql-related issue - however, db-encoding is utf8 too.
any idea? Thanks!
thanks to the helpers! :)

i found that irb does use Syck, rails Psych.
so i guess this is the main difference... will research more..
SOLUTION:
require 'yaml'
YAML::ENGINE.yamler= 'syck'
into boot.rb and BAM!

Maybe this answer can help you:
Ruby on Rails 3, incompatible character encodings: UTF-8 and ASCII-8BIT with i18n
He states that he could solve a similar problem by configuring this:
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
on config/environment.rb
Hope that helps.

Related

Rails 4.0 & Devise - Strong Parameters error

Newbie to rails here, so bear with me.
New app on Rails 4 with ruby 2.0, I installed Devise and followed the instructions(default root, etc). Devise readme on github says it should be compatible with rails4 but
db:migrate failed unless I commented out attr_accessible line in User.rb
After commenting that out, I get "ActiveModel::ForbiddenAttributesError in Devise::RegistrationsController#create" error in trying to create a user.
I see some stack overflow questions like this, but a lot of the answers jump straight into some complex talk. I get I need to specify permitted attributes for mass assignment, but how? And where? And which attributes need to be permitted, all of them? Only those that I expect to be changed/created at the same time?
Judging by the error would I create a registrations_controller.rb that inherits from Devise::registrationsController ? What do I specify in that?
Any step by step, newbie friendly answers are much appreciated. I've exhausted myself trying different code from answers here and various sites from google searches.
Welcome to stackoverflow!
The problem is that the functinality of attr_accessible changed in rails 4.0
2 possibilities to get it running
1 Update Devise that it can handle Rails 4.0
Add this line to your application's Gemfile:
gem 'devise', '3.0.0.rc'
And then execute:
$ bundle
2 Add the old functionality of attr_accessible again to rails 4.0
Try to use attr_accessible and dont comment this out.
Add this line to your application's Gemfile:
gem 'protected_attributes'
And then execute:
$ bundle
Adding below gems and doing a bundle install worked for me
source 'https://rubygems.org'
gem 'rails', '4.0.2'
gem 'bootstrap-sass', '2.3.2.0'
gem 'devise', '3.0.0.rc'
gem 'protected_attributes'
gem 'sqlite3'

rails 3 - LoadError (cannot load such file -- zip/zip)

I'm using rubyzip to zip a csv file so uses can download it. This works perfectly in development mode. But when I tried zipping the file on the production server (rackspace) I received the error: LoadError (cannot load such file -- zip/zip). Is it a path issue? Anyone know a fix?
The error is being called in my code on this line: require 'zip/zip'
I've tried the solution from here, but it didn't help.
I fixed this problem by specifying gem version 0.9.9 in Gemfile:
gem 'rubyzip', "~> 0.9.9"
Using rubyzip (1.0.0) caused an error.
When upgrading rubyzip to 1.0.0 change require 'zip/zip' to require 'zip'.
I had this problem after adding roo to a Rails project.
Roo needed the new interface, something else (some other gem) was using the old interface - so most of these answers didn't work (couldn't lower the version of rubyzip, rubyzip2 is deprecated, didn't have require zip/zip in my project).
What worked for me was cassio-s-cabral's answer referring to the rubyzip github page.
gem 'rubyzip', '>= 1.0.0' # will load new rubyzip version
gem 'zip-zip' # will load compatibility for old rubyzip API.
I had the same problem: error thrown on "require 'zip/zip'" code, and the solution from this post also did not help.
After a long research I found that the problem was that my "require 'zip/zip'" statement was done in a separate
lib/exporters/package_exporter.rb
file, and for some reason "require" statements are not handled in "lib" folder in production by default.
When I moved "require 'zip/zip'" to the beginning of my
app/controllers/packages_controller.rb
the problem was solved!
I had a similar issue with active_support, just added the 'zip' gem to my Gemfile and it worked fine
I'm use rubyzip2 gem to fix this problem
gem 'rubyzip2'
what work for me was to install 2 gems:
gem install rubyzip
gem install zip
and in the script put
require 'rubygems'
require 'zip/zip'
In their github page explains what to do.
Rubyzip interface changed!!! No need to do require "zip/zip" and Zip
prefix in class names removed.
If you have issues with any third-party gems what required old
version of rubyzip you can use next workaround:
gem 'rubyzip', '>= 1.0.0' # will load new rubyzip version
gem 'zip-zip' # will load compatibility for old rubyzip API.

no such file to load -- google_chart using gem gchartrb

I am trying to use the gem gchartrb to create some graphs/charts in my RoR application.
I have looked into several tutorial and all say the same thing, that I have to add
require 'google_chart'
But I am getting the message:
no such file to load -- google_chart
I have the require inside my controller, I have confirmed that the gem is installed.
I am using Rails 3.
Also, I have tried adding config.gem 'gchartrb', :lib => 'google_chart' in my environment.rb as suggested here but nothing changed
Thanks for your help
EDIT:
I have also tried with the gem googlecharts, what I have in my Gemfile is:
gem "googlecharts", :require => "gchart"
but I get no such file to load -- gchart when I try to load the view.
I am not sure, it is required now or not. But it worked for me in Rails 3 as well. I am using Rails 3.0.10. I added below 2 lines and it worked for me.
1) gem 'gchartrb' in Gemfile
2) require 'google_chart' in config/boot.rb
Hope it helps!
config.gem is for rails 2.3.X.
For rails 3, you will need to add the gem to your Gemfile and run gem bundle
You may also need to check that the google_charts gem actually supports Rails 3...
Given that the latest code update seems to have been in 2008 - that might not actually be likely. :(
You can try it anyway and see...

How to disable gem temporarily in Rails 3 using Bundler?

I have discovered that the redgreen gem is incompatible with ISO-8859-1 encoding of test files. What is the best way to disable one gem temporarily to run certain test files?
My first advice would be to save those files in UTF-8.
Alternatively, I'd look into the grouping feature of Bundler.
In your Gemfile
group :utf8 do
gem 'redgreen'
end
And then require that group only when you want to use the redgreen gem.
Bundler.require(:utf8, :default, Rails.env)

Webmock gem in rails 3 and properly including it

I'm likely doing something very simply wrong, but I'm not quite sure what it is. I am porting a rails 2 application to rails 3. This application uses webmock for a bunch of it's tests.
If I include
gem 'webmock'
In my Gemfile, the tests pass, but when I start the server and run the app locally, hitting a controller that should make a web call throws an error:
WebMock::NetConnectNotAllowedError
If I do NOT include the line in my Gemfile, then when I run the app locally, it works fine, but the tests error out with:
`require': no such file to load -- webmock (LoadError)
When this line is hit in my test_helper.rb
require 'webmock'
I'm guessing I've got something configured wrong, but I haven't hit the right google incantation to shed any light on it yet. Where I did I go astray?
Thank you.
Try telling your Gemfile to only load webmock when you're in a test environment:
group :test do
gem "webmock"
end
On my Ruby 1.9 Rails 3 instance I have something like the following:
group :test do
gem "mocha"
gem "webmock"
end
group :development do
gem 'ruby-debug19', :require => 'ruby-debug'
end