rails encoding issue =( - ruby-on-rails

i use rails 2.3.9 with ruby 1.9.2 and when i trying to update my model with some russian letters i have error in unicorn log:
Error during failsafe response: incompatible character encodings: UTF-8 and ASCII-8BIT
Read error: #<NoMethodError: undefined method `[]' for nil:NilClass>
trace look like this:
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:521:in
process_client'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:594:in
block in worker_loop'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:592:in
each'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:592:in
worker_loop'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:482:in
block (2 levels) in
spawn_missing_workers'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:479:in
fork'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:479:in
block in spawn_missing_workers'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:475:in
each'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:475:in
spawn_missing_workers'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:489:in
maintain_worker_count'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn/http_server.rb:299:in
join'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/lib/unicorn.rb:13:in
run'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/gems/unicorn-3.0.1/bin/unicorn_rails:208:in
<top (required)>'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/bin/unicorn_rails:19:in
load'
/home/rbdev/.rvm/gems/ruby-1.9.2-p0#rails2/bin/unicorn_rails:19:in
`'
so, i can't detirminate the problem, the only thing i know - what problem in russia text ( when i update model with english letters - all is ok. what i can do ? (

You should ensure that your editor saves files in UTF-8. ASCII afaik is the first part of any charset. That should be the reason you don't get any errors when you leave out the russian chars.

I answered this one here, with a script. Why are all strings ASCII-8BIT after I upgraded to Rails 3?
You need
# coding: UTF-8
at the top of your files, with ruby 1.9. If that doesn't help, it might be your external dependency, such as DB.

you can use gem "russian" (gem is based on l18n)
https://github.com/yaroslav/russian
in model (instead of russian letters):
Russian::translate(:some_word)
in config file (must be encoded in UTF-8):
ru:
some_word: 'это строка с русскими буквами (this is string with russian letters)'

Related

No implicit conversion of Nil to String Error with Paperclip

In my app I've created a view which lists all the PDFs in my S3 Bucket, and for this I am using the Paperclip Gem. However I am now receiving this error in my terminal.
Error performing Courts::SyncronizeBucketJob (Job ID: 8424356c-9717-4cca-9d76-4b157bab1065) from DelayedJob(default) in 450.5ms: TypeError (no implicit conversion of nil into String):
/Users/conorquarry/RubymineProjects/pair/app/_modules/courts/models/ocr_document.rb:46:in `initialize'
/Users/conorquarry/RubymineProjects/pair/app/_modules/courts/models/ocr_document.rb:46:in `new'
/Users/conorquarry/RubymineProjects/pair/app/_modules/courts/models/ocr_document.rb:46:in `block in <class:OcrDocument>'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/interpolations.rb:35:in `block (2 levels) in interpolate'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/interpolations.rb:35:in `gsub!'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/interpolations.rb:35:in `block in interpolate'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/interpolations.rb:34:in `each'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/interpolations.rb:34:in `interpolate'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/attachment.rb:556:in `interpolate'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/attachment.rb:171:in `path'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/storage/s3.rb:355:in `block in flush_writes'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/storage/s3.rb:352:in `each'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/storage/s3.rb:352:in `flush_writes'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/attachment.rb:247:in `save'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-meta-3.1.0/lib/paperclip-meta/attachment.rb:13:in `save'
Looking at the Paperclip Interpolation Documentation I can't see my error, does anyone have an idea of where I should start? Below is the code throwing me the error.
Paperclip.interpolates :document_folder_name do |a, _s|
Pathname.new(a.instance.object_key).dirname.to_s
end
The error is reproducible by:
Pathname.new(nil)
So in this situation, a.instance.object_key is nil.
From the interpolations doc, a.instance will be the activerecord (or whatever ORM you're using) model that's trying to generate a paperclip path that includes :document_folder_name in it.
I'd guess #object_key is a method defined on your activerecord model and is returning nil.
You can put a pry / debugger or a print statement inside that block to see what the object is, then it should be fairly easy to write a unit spec for that method to ensure it returns the correct string.
In theory calling object_key.to_s would fix the error but would mean that :document_folder_name would interpolate to an empty string when paperclip is calculating the folder path, which may not be desirable.

ActionDispatch::ParamsParser replacement after upgrade to Rails 5

Migrating an app from Rails 4.2.9 to 5.2.1.
This is latest issue:
$ rails console
/Users/meltemi/rails/myapp/config/initializers/disable_xml_params.rb:3:in `<top (required)>': uninitialized constant ActionDispatch::ParamsParser (NameError)
from /Users/meltemi/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/engine.rb:657:in `block in load_config_initializer'
from /Users/meltemi/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activesupport-5.2.1/lib/active_support/notifications.rb:170:in `instrument'
from /Users/meltemi/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/engine.rb:656:in `load_config_initializer'
The offending line of code in an initializer:
# config/initializers/disable_xml_params.rb
ActionDispatch::ParamsParser::DEFAULT_PARSERS.delete(Mime::XML)
Rails Guides says:
ActionDispatch::ParamsParser is deprecated and was removed from the middleware stack. To configure the parameter parsers use ActionDispatch::Request.parameter_parsers=. (commit, commit)
So I've tried the following:
ActionDispatch::Request.parameter_parsers.delete(Mime::XML)
But that begets more errors:
$ rails console
/Users/meltemi/rails/myapp/config/initializers/disable_xml_params.rb:3:in `<top (required)>': uninitialized constant Mime::XML (NameError)
from /Users/meltemi/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/engine.rb:657:in `block in load_config_initializer'
from /Users/meltemi/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activesupport-5.2.1/lib/active_support/notifications.rb:170:in `instrument'
from /Users/meltemi/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/railties-5.2.1/lib/rails/engine.rb:656:in `load_config_initializer'
Is there a better way to call .delete on that object?
You have to grab the existing values from parameter_parsers, modify it to suit your needs, then reset the values to your modified values. From the ActionDispatch::Http::Parameters documentation:
parameter_parsers=(parsers)
Configure the parameter parser for a given MIME type.
It accepts a hash where the key is the symbol of the MIME type and the value is a proc.
original_parsers = ActionDispatch::Request.parameter_parsers
xml_parser = -> (raw_post) { Hash.from_xml(raw_post) || {} }
new_parsers = original_parsers.merge(xml: xml_parser)
ActionDispatch::Request.parameter_parsers = new_parsers
In your specific case, you should look at the parsers in original_parsers to see if there is anything to delete. On a simple Rails 5 app that I have handy to look at, the only values I have are:
=> {
:json => #<Proc:0x00007fe818fc6fb8#/Users/foo/.rvm/gems/ruby-2.6.0-preview2/gems/actionpack-5.2.1/lib/action_dispatch/http/parameters.rb:11 (lambda)>
}
Your app's configuration is likely different, but to answer your question about how to delete a value, this simple version should work:
ActionDispatch::Request.parameter_parsers = ActionDispatch::Request.parameter_parsers.except(:json)
You may find additional useful information in this answer.

Why is gsub complaining about nil when my object isn't nil?

I’m using Rails 4.2.7 and have the following to trim all white space from a particular expression …
puts "division: #{division}$$"
division = division.gsub(/\A\p{Space}+|\p{Space}+\z/, '')
when I run this, eventually my program dies with this output …
division: 18-29$$
Error during processing: undefined method `gsub' for nil:NilClass
/Users/mikeb/Documents/workspace/runtrax/app/models/race_time.rb:27:in `block in <class:RaceTime>'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:448:in `instance_exec'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:448:in `block in make_lambda'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:228:in `block in halting_and_conditional'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:506:in `block in call'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:506:in `each'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:506:in `call'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
…
I don’t understand why gsub thinks its getting a nil when the line above, the string is clearly not empty (a value of 18-29 is printed out). Curiously, I can’t reproduce this in a Rails console, so I’m wondering it here’s something else going on or if there’s a better way to write the above.
Some thoughts:
gsub is a method on strings. test out the type for "division" with this: puts division.class you may find that while "division" is not "nil," it may not be a string so it's throwing an error. see HERE for more description.
since your code is meant to change the value of division, you can do so more elegantly and economically using a "destructive method" as follows: division.gsub!(/\A\p{Space}+|\p{Space}+\z/, '') See HERE for a discussion of destructive methods (note the "!", also called "bang"). This probably doesn't solve the original question, but it does pretty up your code.

In watir-webdirver gem '.click' is throwing error in windows 10 but working fine in linux(Ubuntu)

I am using watir-webdriver gem to run over a website.
I tried to click a button to load more content in a webpage, for that I used the following code:
b = Watir::Browser.new
b.goto "https://www.example.com/"
b.button(:class => "ga_Finder_Load_More").click
This code is running fine in linux(ubuntu) and I can load the data in the webpage by clicking the button. But when I run the same code in window 10 I am getting the following error,
C:/Ruby200/lib/ruby/gems/2.0.0/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:508:in `assert_exists' from C:/Ruby200/lib/ruby/gems/2.0.0/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:114:in `click' from leafly_v1.3.rb:70:in `block (2 levels) in <main>' from leafly_v1.3.rb:67:in `each' from leafly_v1.3.rb:67:in `block in <main>' from C:/Ruby200/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.4-x86-mingw32/lib/nokogiri/xml/node_set.rb:187:in `block in each' from C:/Ruby200/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.4-x86-mingw32/lib/nokogiri/xml/node_set.rb:186:in `upto' from C:/Ruby200/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.4-x86-mingw32/lib/nokogiri/xml/node_set.rb:186:in `each' from leafly_v1.3.rb:62:in `<main>' C:\Ruby200>
Ruby version - 2.0.0
watir-webdriver - 0.9.1
Can anyone please help me to fix this or can any one suggest me alternate method to click a button.
Thanks.
It often happens between different operating systems and browsers that pages will load with slightly different timing. Try:
b.button(:class => "ga_Finder_Load_More").when_present.click

Vim jump to a file using ctr-w gF with a # in the path like dirwith#funnyname/path:34

My stack trace errors in ruby on rails look like below. I have happily used ctr-w gF to jump to the file and go to the line number following the file for a while.
However I started using RVM and gem_sets, and this puts a # symbol in the dir which apparently confuses Vim. How can I use my beloved ctr-w gF while this is the case?
/Users/x/.rvm/gems/ruby-1.8.7-p334#idance/gems/activesupport-3.0.7/lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/x/.rvm/gems/ruby-1.8.7-p334#idance/gems/arel-2.0.10/lib/arel/table.rb:103:in `[]'
/Users/x/.rvm/gems/ruby-1.8.7-p334#idance/gems/activerecord-3.0.7/lib/active_record/counter_cache.rb:34:in `reset_counters'
/Users/x/.rvm/gems/ruby-1.8.7-p334#idance/gems/activerecord-3.0.7/lib/active_record/counter_cache.rb:19:in `each'
/Users/x/.rvm/gems/ruby-1.8.7-p334#idance/gems/activerecord-3.0.7/lib/active_record/counter_cache.rb:19:in `reset_counters'
Include the # character in 'isfname'.
:set isf+=#-#
For more information:
:h 'isfname'

Resources