ruby on rails

Carrierwave mini magick error

Been using paperclip mainly for file attachment in rails application. When there is a need to add in file uploading feature for a small apps in the company, I decided to give carrierwave a spin, just for the sake of trying out a different gem.

I mainly refer to carrierwave github site and tutsplus tutorial  for installation and setup, which is pretty straightforward to be frank.

However, when I tried to upload image thru the application, an error message saying “translation missing: en.errors.messages.mini_magick_processing_error” appear.

A quick google confirm that it is quite a common problem, a few suggestions are made,

But none of these solve my problem. Suddenly it hit me that maybe ImageMagick is not working properly, I did a check by just type in magick command into the command prompt, sure enough it said magick is not a recognizable command.

As suggested by ImageMagick page , look like I need to install Visual C++ 2013 Redistributable Package  to get the file vcomp120.dll.

And viola, that did the magick trick and solve the problem J

Also, if the name of “command prompt” haven’t given enough clue, I am developing rails apps with a Windows machine. Hope this could be a useful tips for those need to make rails apps and carrierwave work in windows 😉

ruby ruby on rails

Testing in Sublime

Following railstutorial again, to strengthen my rails basic and pick up new shiny things in rails 4.

Getting sublime to run test directly is quite awesome, I mean you just need to highlight part of the test you wish to run, cmd + shift + R, boom you got your test result, all without needing to leave the comfort of sublime. Sweet.

On the next day though, suddenly it refuse to run, complaining

/bin/sh: rspec: command not found

Stackoverflow answer here go for this one. But a simpler solution below work for me, just go to ~/.config/sublime-text-2/Packages/RubyTest/RubyTest.sublime-settings file and set configuration option “check_for_rvm” to true and you have your mojo back.


ruby on rails

Getting paperclip, heroku and aws s3 non-us region bucket working together

As I have found out earlier, images uploaded via paperclip to heroku will go missing after each push of code to heroku.

One way to keep images uploaded via paperclip permanent is to use a 3rd party storage service like s3 or keep it on a ftp server. I went for s3, mainly want to learn dealing with Amazon Web Services, might turn to ftp server later on if needed. If you would like to go the ftp server way, there are gem like FTP Storage for Paperclip and PaperclipFTP to make life easier.

  1. Add paperclip and aws-sdk to your gemfile
    gem 'paperclip'
    gem 'aws-sdk'

    Follow by the usual

    bundle install
  2. To start working with s3, first you need to signup for an aws account.
  3. Once you have an account, login then go to My Account/Console > AWS Management Console
  4. Select s3 from a host of services
  5. Create a bucket
    “A bucket is a container for objects stored in Amazon S3. When creating a bucket, you can choose a Region to optimize for latency”, that is what mentioned in the guide, so I chose Singapore as it is closest to my location.
    Now if you have chosen “US standard” region, things will work out much straightforward, any non-us region will need some tweaks in the configuration. The documentation in heroku did mentioned that some international users may need to override the default URL structure and place the bucket’s name “domain-style” in the URL. Following that guide didn’t solve my problem though. I googled around, found a few more useful guides

    • Stackoverflow on setting env variables
    • Techspry on s3 configuation
    • DCChua on how to get Paperclip and AWS-S3 Singapore (and European) buckets working

    Coupled with the heroku documentation, I finally get my setup working combining them and change a bit here and there.

  6. Get your aws access key and secret access key from the Security Credential option
  7. In ./config/production.rb, defined this environment variables
      config.paperclip_defaults = {
    			    :storage => :s3,
    			    :s3_credentials => {
    			      :bucket => ENV['AWS_BUCKET'],
    			      :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    			      :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
    			    :path => ":class/:id/:basename_:style.:extension",
    			    :url => ":s3_sg_url"
  8. Then set them by doing these in terminal from your apps
    heroku config:set AWS_BUCKET=your_bucket_name
    heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id
    heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key

    You can of course set these values in the ./config/production.rb file, but then it is not protected if your repo is not private.

  9. In ./config/initializer, create paperclip.rb, and add these lines
    Paperclip.interpolates(:s3_sg_url) do |att, style| 

git commit, push and then push to heroku, images uploaded from your apps should now store in s3.

ruby on rails

Uploaded images go missing in heroku

No doubt heroku is the greatest thing since sliced bread, with easy deployment and stuff, and it is free to start with. Perfect for stingy developer like me. So I am working on a rails apps, deployed to heroku, with paperclip for image upload. Things work fine, except images I uploaded keep on missing. I thought heroku wouldn’t store image for free user, ha.

A quick search around internet, sure enough it is stackoverflow come with an answer top in the list. It turn out that only images in the commit and pushed to heroku are kept, example logo or icon used in the apps. Uploaded images will be removed with each push to heroku, clearly images uploaded via paperclip fall into the latter category.

Solution is to use a 3rd party storage, like Amazon Simple Storage Service. Of course, aws is free to start with as well, a free account includes 5Gb storage, 20k Get requests, 2k put request, yada yada, another great thing for stingy developer, hurray!

ruby on rails

Autorun mess up bundle install

I still feel like working with rails in Linux feel easier and faster. What’s with all the homebrew thing in Mac, feel like you have to go through a lot of troubles just to complete something simple. But working with rails in Windows is another story altogether. Initiatives like Railsinstaller and Devkit help a lot to get Windows to be less of a pain, but still…

So I have install railsinstaller, and Devkit come installed with it. And I have things running pretty normal, till lately I try to create a new rails apps, doing a bundle install lead to error saying

Gem files will remain installed in C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9 .1/gems/json-1.7.6 for inspection. 
Results logged to C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/json-1.7. 6/ext/json/ext/generator/gem_make.out An error occured while installing json (1.7.6), and Bundler cannot continue. Make sure that gem install json -v '1.7.6' succeeds before bundling.

I tried to reinstall railsinstaller, and manually install Devkit. No working. Then tried to uninstall railsinstaller, and have ruby installer manually, then Devkit, make sure Devkit path is under ruby root, still the problem persist. Finally I found the solution in stackoverflow, from a link in treehouse’s blog . The thing is Devkit doesn’t work with Autorun interfaces, so if you have previously have this setup, in my case it was the I setup for syntax highlighting in command prompt, then you will have to remove it.

Just type this into command prompt

REG QUERY "HKCU\Software\Microsoft\Command Processor"
REG QUERY "HKLM\Software\Microsoft\Command Processor"

Should get something like below

HKEY_CURRENT_USER\Software\Microsoft\Command Processor
    CompletionChar        REG_DWORD    0x9
    DefaultColor          REG_DWORD    0x0
    EnableExtensions      REG_DWORD    0x1
    PathCompletionChar    REG_DWORD    0x9

These columns actually stand for Key, Type and Value. If there is a Key named AutoRun, then this could be the root cause of the problem. In my case, I think the Autorun key was set when I setup ansicon.
Run this to remove it,

REG DELETE "HKCU\Software\Microsoft\Command Processor" /v AutoRun

Close command prompt, and bundle install again in a new command prompt session, things should work.

ruby on rails

Futnotes running locally

Redo the whole process. Clone repo again, bundle install now work right away, as all the prerequisites are there.
Instead of running rake tasks separately, which fail previously with long list of errors

rake db:create:all
rake db:migrate
rake db:seed

I will just do this, which is a shortcut for rake db:create; rake db:schema:load, and rake db:seed

rake db:setup

And it work!

Precompile Assets,

rake assets:precompile

lead to an error saying “Segmentation fault while running ‘rake assets procompile'”. A suggestion in stackoverflow saying that the rootcause might be execjs, change to therubyracer gem should solve the problem. Even though there is no execjs gem dependency in the gemfile, I still add in therubyracer gem, bundle install again, now rake assets:precompile just work, and I have futnotes running locally. Now off to get the test done

ruby on rails

Futnotes – setting up the repo

Cloning the repo

git clone devtest

Mixtures of errors doing bundle install though
First it is capybara

Using capybara (1.1.2) 
Installing capybara-webkit (0.12.1) with native extensions 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

According to these sources thoughtbot and stackoverflow qt need to be installed first, before install capybara.
To install qt, you must link to libpng, simply link to linpng will fail due to permission,

Error: Could not symlink file: /usr/local/Cellar/libpng/1.5.13/lib/pkgconfig/libpng15.pc
/usr/local/lib/pkgconfig is not writable. You should change its permissions.

To fix that

sudo chown -R kahfei /usr/local/lib/pkgconfig

then only follow by command below

brew update
brew link libpng
brew install qt

bundle install still fail though

An error occurred while installing rmagick (2.13.1), and Bundler cannot continue.
Make sure that `gem install rmagick -v '2.13.1'` succeeds before bundling.

It seems that lot of people having this same problem. Some of the suggestion work for some people, but none of it fixed my problem. Apparently this is a rmagick bug, as it does not support the newer version of ImageMagick. rmagick has not been updated for two years, so if there is an option, change to other gem to work with image, like minimagick

After hours of scouring the internet, and trying a few different approaches, using the magick-installer to install ImageMagick, then

bundle install

work again. No actually I lie,  bundle install yield another error

ERROR: While executing gem … (ArgumentError) marshal data too short

There are suggestions to remove ~./gem folder altogether and reinstall all the gem. But in my case, bundle install work on other rails apps, just not this one. So I clone again the repo to a new folder, now bundle install work!

Now creating development, testing and production environment. I already have postgresql installed locally, so I will use postgresql, my database.yml looks something like this

adapter: postgresql
encoding: unicode
database: futnotes_development
pool: 5
username: kahfei
adapter: postgresql
encoding: unicode
database: futnotes_test
pool: 5
username: kahfei
adapter: postgresql
encoding: unicode
database: futnotes_production
pool: 5
username: kahfei

now to create them

rake db:create:all

All three database created

rake db:migrate

with a long list of errors, it start with something like this

PG::Error: ERROR:  column "position" does not exist
LINE 1: ...RE "competitions"."name" = 'Premiership' ORDER BY position, ...
ruby on rails

How this started

I think it was late 2005, my wife used to work really late during those days, 8, 9 even 10pm at night, I have experienced a few even longer wait until 1 or 2 am, literally the next day. Sometimes I use this spare time to finish up my works, but I was still quite junior then, there aren’t much work that I can’t finish in normal working hour.

As a system administrator for the department, one of the tasks that I dratted most is to update a long list of computer inventory. Physically go to a computer, check on serial number, OS installed, computer name, application installed, etc etc. If the user was not around and the screen locked, then I am out of luck and have to find another time for a check. So I punched in words like “hack”,”hack remotely to check hardware specification” into google, hope it will yield some useful dos-prompt command that let me remotely checking out the hardware specification. Instead, I came across Eric Steven Raymond epic article “How to become a Hacker”. One thing lead to another, soon I find myself reading every Paul Graham’s essay feverishly, Great Hackers, Hackers and Painter, The Word Hacker, How to do what you love, Python paradox , the list go on.

Reading “How to become a Hacker” and most of the PG’s essays is mind blowing, like open up a whole new world, at a time when hacker normally carried a negative connotation in the mainstream; suddenly I was shown the true meaning of a hacker.

“Hackers solve problems and build things, and they believe in freedom and voluntary mutual help”
“hacker” connotes mastery in the most literal sense: someone who can make a computer do what he wants.
You do something so clever that you somehow beat the system, that’s called a hack

Wow, that sounds cool. First thing in the basic hacking skills listed in “How to be a Hacker” is to learn how to program. As a Computer Science Graduate, sadly all I have learned during my study was some asp for coursework, html and really tiny bit of java. I still remember when we learn html, I put up a page with <marquee> tag showing off a blinking words moving across screen then proudly showoff to my classmate. Serious.

So I have to pick up a language and brush up my programming skills again. Since both Eric Steven Raymond and Paul Graham touted Python as one of the language to go for, that was what I did. Learning python with A Byte of Python, Dive into Python and The Python Tutorial in IDLE make those waiting hours gone past so much easier.

Feeling excited with the new found world, I continue to play with django, ruby on rails, Processing, Fedora Core, Ubuntu, wordpress, radiantcms, to name a few. I was fascinated with the newer shinny things churning out from the web. It is always the same pattern, I started out really eager, then either something happen in real life, change job, married, have kid, etc, or some newer toys show up, then I shift my attention. I spent a lot of time learning, but with no end result to show. It is a cycle that dragged for a few years and brings me to nowhere.

Until a casual chat with my childhood friend some time last year, we rant about the meaningless job that we are doing, how I really long for works that require my creativity, be in coding or design. That must be the hundredth time we went thru the same rants. But this time, my friend said, “You are in your thirties, your life is almost fixed. You are done. You can forget about your bloody hacker’s dream and just think about your son’s future instead.”

The thinking that my life is done, and I can’t do anything about my future, that I am gonna be the same mindless working robot in corporate working life until I retire suddenly strike me.

Is that what I really want? Admit defeat without even try hard enough? Then what is the lesson I am teaching my son here? How am I supposed to tell him that you can be all that you want to be as long as you work hard enough? What kind of example I am setting for him?

Being in my thirties is exactly the reason I should really buck up and deliver; I have no time to waste. So to force myself to deliver, I looks for excuse to rollout Ruby on Rails application to the department, eventually I find two, and commit a date that I don’t really feel comfortable with, then push myself to build it, sometime even drop my job on hand totally to make sure I delivered the features and meet the rollout date. It took me about 6 weeks working on and off, eventually I delivered both applications to about 50 users. Railscast, stackoverflow, AWDWR 4th Edition were my most frequently used references throughout the development. You really learn a lot more building up real things compare to just reading tutorial or books and creating bogus websites.

I enjoy every minutes of it, coding in rails, solving problem, changing layout, adding in a bit of jQuery. Everything. But then every minute that I have to perform my normal task become so much more unbearable. I really need to do more real works in rails.

So I search almost every job sites. There are not a lot of rails opening in my area (Penang, Malaysia), and even if there is, it might be difficult for me to find a permanent job in ruby on rails that could pay my current salary. After all I am experienced (7 years) in my current field as a system admin, but in term of rolling out rails apps, I have only the experience of developed two rather small rails apps for 50 users to show, as for rails apps that facing the real world …none.

So when I saw a remote rails developer job opening, I just have to give it a shot. There is a test project as a prerequisite for the job. I thought it could be completed over a weekend, but it took a little bit longer than I wished, end up I decided to take a day off from my work to complete it. Don’t want to regret for not trying hard enough. I learn something even with this small little test project, deploying to engineyard, brush up my regex, pick up some jquery.

After completing the test project, although the employer commending on the effort I put on, he couldn’t take me as he is looking for a permanent fulltime remote employee, not a part-timer. And he doesn’t have the budget to match my current fulltime salary.

I tell him frankly I might not have a lot of real world rails experience, but I really like web development and would love to have the opportunity to work on the project. So we discussed about the possibility of a part time arrangement on hourly rate. We come to a rate that is much lower than if I were to divide my fulltime salary by the hour. But heck, I am starting out and just glad to get my first freelance rails gig.

So far I have been in it for about 2 months, I work on the project by waking up early at 4 or 5 a.m. before going to work on weekday, then a total of 8-10 hours on weekend. Learn a lot of things like version controlling in git, deploying to heroku, which I wouldn’t have pick it up as fast if I am not developing a real apps. Really love every minute of it, and ya, that make going back to my fulltime work even more difficult, just wish to spend more time on the project.

It is tough for me to make a sudden switch of career with all the commitment I have, as I need a stable income. But I intend to keep pushing and not submit to the ideas that my life is done at thirties. Now I am just looking for ways to add experiences and repertoire to my web development skills, while pilling up my github profile. I believe the best way to do that is by doing real work. Hopefully that will open up more doors for me. I know this must be cliché to quote this words now, but still,

“Stay hungry, stay foolish. Keep looking, don’t settle”.

ruby on rails

destroy every record in a table in rails console

This is really useful, as I keep googling it everytime I wanna do this with the rails console…


Got it from the comment section from this post

A much shorter way of doing so, as comment by skim,

ruby on rails

remote_user in rails application

I am developing a little application within instantrails, suppose to work in an intranet environment.

The application let user book a part number, by filling some information. For better user experience, I plan to capture the username at the other end that make the booking, instead of creating another layer of login feature and require the user to login again. There is no complex authentication needed, just capture the username and store it together with other information.

ENV[‘user’] will give you the username that has the rails application running, which is not what we want.
ENV[‘remote_user’] return nothing at all.
I read through quite a few discussions on the net, it seems like apache didn’t actually pass the remote_user value to mongrel, hence rails application couldn’t call out remote_user. To fix that, we will need to modify on the apache configuration file, httpd.conf, and .htaccess. Admittedly, I really know close to nothing about the setup of these files, and have a hard time following the suggested steps, as to where to add the suggested line of codes, which file to modify, etc, etc.

So I figure, if we forgo apache in the setup, will it be easier to fetch the remote_user value directly from mongrel? or from thin if it is the web server being used.

But still, I just couldn’t get it working.
Is there any gem or plugin need to be installed before this remote_user tingy can be pass to rails application?