Rails encoding under Cygwin - ruby-on-rails

I'm trying to develop a rails app under Cygwin and Eclipse. I use Ruby 1.9 and Rails 4.1.
I made sure, using recode, that all my files have the Windows-1252 encoding and my Eclipse project uses the same. I've also tried to use UTF8 in both places but this error keeps showing up:
incompatible encoding regexp match (Windows-1252 regexp with UTF-8 string)
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
Is this related to my Rails/Ruby (read, Rails source files being in the Windows encoding while my code is in UTF-8) setup under Cygwin?
Rails Stacktrace

Add
# encoding: utf-8
to the top of every ruby source file

I found this link, which walks you through installing Rails via Cygwin on Windows. I know you already have it installed, but it includes making Cygwin recognize UTF-8 encoding.
http://collaborate.je/2014/08/setting-ruby-rails-windows-7-via-cygwin/
I do not know if you are using Windows at work or home. If the latter, you may want to spend a little time installing Ubuntu on VirtualBox. You would only need a few GB of space, with an entire Linux OS running as an app within Windows. This is ideal for Rails apps, as many tutorials recommend Unix, and even more companies use Unix to program and run servers.
(In case you don't know, Linux falls under the Unix category, which also includes Mac. Windows is difficult to work program in certain languages, because its kernel and directory structure are not designed the same as Unix. But, I am sure you know this. :-) )

Related

Reverse engineering what version of ruby and rails a project was developed under

Some background:
I am new to Ruby and Rails and I've been assigned to get an already completed Ruby on Rails project to compile from source.
The project has, as far as I've been able to discover, little to no documentation on how to set it up. The developers are unavailable for me to contact.
After doing some tutorials and learning the basics I have been trying to get the code to compile and run. My platform/setup is currently:
Windows 7
Ruby 1.9.3
Rails 3.2.13
Although I am attempting to develop with a VirtualBox Ubuntu setup alongside of this because I suspect it will be easier in the long run.
Despite there being no Gemfile for this project I have managed to (I believe) pin down and install all of the necessary gem packages (hopefully compatible versions). I am now running into this issue:
in alias_method': undefined methodpath' for class `ActionController::UploadedStringIO' (NameError)
As far as my searches have led me to believe this is a bug that can occur when the versions of Ruby and Rails are not correct/incompatible?
Is there a way to "reverse engineer" what version of Ruby and Rails was used to develop this project in the first place from the code alone? Could this bug be caused by me using 3.2.13 Rails if the original developers were using 1.8.7 Ruby? It seems to me that if I can emulate their setup closely enough then the source should compile and I can get down to business.
Additionally I am using the default WEBrick server. Is there a way to determine what the original team used for the web service? Does it even matter if they used a Apache setup or are these server implementations mostly interchangeable aside from efficiency?
Thank you for your time. If you have any further advice on how to handle this sort of project I'd love to hear that too.
If there is no Gemfile, it points to the Rails app being 2.3 or earlier. As for the Web server, they are interchangeable, but there is really very little chance that they were using Webrick, due to its ability (or lack thereof) to handle many concurrent requests. Chances are, they were using mongrel, or passenger via Apache or Nginx.

Ruby on Rails command line looks weird

I just began learning RoR using the materials from railstutorial.org. When the author uses the command line, the resulting text are always nicely organized and colored. When I do the same thing, I often get "weird" characters and unorganized text as result.
http://i.imgur.com/2Q0kzwf.jpg
Example
http://i.imgur.com/mZP4SI9.jpg
My attempt to do the same
I'm not quite sure what to do to make my command line more organized like the one shown in the tutorial. Any help would be appreciated. In case you need to know, I'm using Windows 7.
It is because by default Windows command prompt doesn't know about ANSI color Sequences. You can try https://github.com/adoxa/ansicon which is supposed to make it aware. The reason the tutorial authors look like that is they are using a *nix shell that understands the color codes (probably bash or zsh on either linux or osx if I had to guess)
As Doon pointed out, the Windows console doesn't recognize ANSI sequences. As suggested your best bet right now is to use external tools.
However, since there are some pain points using most of the external tools, Ruby 2.0 will provide support for ANSI escape codes in Ruby out-of-the-box without having to depend on external gems or tools.

How do I incorporate an independently maintained ruby script into a rails application

I have a ruby script (.rb) that opens a file and makes a csv file after some parsing.
I maintain the script independently and may use in other applications.
Right now I just copied and pasted the code into my controller... I know that isn't right!
How am I supposed to incorporate this ruby script to my application?
Do I make it a gem?
Thanks.
Making it a gem and installing it is one option. Otherwise, register the directory of the file as load path. That part may depend on the operating system. For example, with Ubuntu Linux, I do in the terminal:
export RUBYLIB=path_to_the_directory_where_the_file_is
Then, require that file and use it. When you want your library to behave differently depending on if it was called directly from the command or from another ruby script, the common way is to write in your library:
if __FILE__ == $0
commands_to_execute_when_called_directly_from_command
end
We write little Ruby-based command-line tools all the time, and treat them as regular Linux apps. It's trivial to call them using back ticks or %x, or chain them using regular pipes (|) as we would a regular app.
If we're going to be throwing a lot of data around, often we'll add a --json flag using OptionParser, which lets us emit JSON to the calling program, making it easier to reuse the data instead of having to parse CSV or columnar data.
You can install those sort of apps in /usr/local/bin on a *nix system, make sure the path is set to search there, and then share the code among shell, Ruby or any other language capable of using a sub-shell.
Just because they're written in Ruby doesn't mean they have to be a gem or module. Ruby is capable of much more than that and fits into the usual host ecology well.

Ruby Character Encoding Confusion When Reading Same File In Different Environments

I have a Rails application that accepts file uploads of CSV files. When developing the feature locally on my Mac, I received an "invalid byte sequence in UTF-8" error when trying to parse the uploaded file (using Ruby's standard library CSV).
So after doing some research and reading some answers to similar questions on StackOverflow, I tried using a gem to sniff out the character encoding (namely CharDet), and then when opening the file via the CSV library, I would specify the encoding. And this solved all my problems, and life was good.
content = File.read(fullpath)
self.file_encoding = CharDet.detect(content)['encoding']
CSV.table(fullpath, :encoding => file_encoding, :header_converters => :downcase).headers
But then I deployed this code to the production Linux environment, and again with the "invalid byte sequence in UTF-8" errors. What a mystery (to me anyway)! After quite some time trying to resolve the error, I tried removing the code that specified the encoding upon opening the file. And miraculously it fixed the problem on production, but now local Mac development is broken.
Keep in mind, that in both cases I'm uploading the same file using the same browser. Does anyone have any insight on what is going on here?
By the way, versions of ruby are close, but not the same. The Mac is ruby 1.9.3-p0, and the Linux server is 1.9.2-p180. The app is Rails 3.2.6.
A few thoughts:
Have you confirmed the encoding of the file that you're uploading?
Have you tested with 1.9.2-p180 on your Mac, as Frederick Cheung suggested?
Have you tried outputting the results of CharDet.detect on each platform to see what the encoding of the received file (as opposed to the uploaded file) is? I wonder if some configuration is different between Apache on Linux and WEBrick on your Mac?
Are you using the same version of CharDet on both platforms? What libraries does it use (e.g. iconv), and are they the same version on both platforms?
I'm not aware of any differences in behavior with regard to encoding between 1.9.2 and 1.9.3, but I haven't specifically researched it either. It could also be a difference in the configuration of the MRI build.

Force Ruby Version

I just got burned because I used find_index on an array on my dev box (OSX with Ruby 1.8.7) and the deployment machine runs Ruby 1.8.6. (What's the difference between find_index and index? The latter works on 1.8.7 and 1.8.6)
So that got me thinking: what's the best way to force Rails to run with a specific Ruby version?
Since it's probably relevant (install multiple rubys!), I need to know this for OSX, but it would be useful to know for Linux, Windows, and Commodore 64 as well.
Later: Of course I'm working in a virtual appliance now, but I'd like to be able to control my Ruby versions if possible on my computer.
Note: I don't care too much disallowing Rails running with the wrong Ruby version. I'm more interested in getting the RIGHT ruby version to run. Sorry for the confusion.
This won't force the version of ruby required but you may want to utilize something like RVM to easily manage your ruby environment on your dev and production boxes.
http://rvm.io/
This allows you to easily switch and maintain multiple versions of ruby on your system.
This is brute force and ignorance, but one approach would be
raise "Wrong ruby version, please use ruby 1.8.7" unless RUBY_VERSION == "1.8.7"
Use the RUBY_VERSION constant in your Application controller.
This shows rendering the 500 error page. You would want to setup a new page in your public dir with an appropriate message.
before_filter :check_ruby_version
def check_ruby_version
unless RUBY_VERSION == "1.8.7"
render :file => File.join(Rails.public_path, '500.html'), :status => 500
end
end
Another way to look at the problem would be to be able to disregard differences in the version of Ruby you're running. My backports gem brings Ruby 1.8.6 up to date in the 1.8.x line (including the upcoming 1.8.8) and much of 1.9:
require "backports"
Or instead, for the less courageous among us, you can require only 1.8.7's features:
require "backports/1.8.7"

Resources