Pages - Menu

Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts

Saturday, October 12, 2013

Friday, October 11, 2013

Setting up a Rails environment with RVM

Hi, happy? I hope so :)

So here I'll show you how to setup a Rails environment using RVM, if you are a Windows user, I'm sorry, RVM is not for you but you can do it using a VM.

We are going to install RVM that stands for Ruby Version Manager and create some Gemsets with different Rails, Ruby and Gem versions.

Install RVM

$ curl -L https://get.rvm.io | bash -s stable

You can also append some options like --rails, --ruby or specify the version like --ruby=1.9.3.

After the installation there will be a folder .rvm on your home directory, that's where all the magic will happen. If something goes wrong in the future you can remove that folder and install again but all your Gemsets, Gems and Rubies will be gone as well, be careful.

When the installtion is over it will automatically update your .bash_profile adding this:

source $HOME/.rvm/scripts/rvm

Which will make your RVM recognizable on your terminal as a function, otherwise it won't work. If for some reason it doesn't happen, you can add it by yourself like:

echo "source $HOME/.rvm/scripts/rvm" >> ~/.bash_profile

When that's done you must reload your dotfile so the new line take effect:

. ~/.bash_profile

Or you can close and reopen your terminal :)

OK, now you have RVM working as a function and the next step is to check your OS requirements for it to work.

rvm requirements


In my case, using Manjaro, it has no requirements but when it does it will start downloading and in some cases it must be run as sudo.

To check what is available for you to use like ruby, jruby, macruby, etc:

rvm list known


All those options are available for you to install like this:

rvm install your_option_goes_here

So let's install Ruby 1.9.3. As you can see it has 1.9.3[-p448]

rvm install 1.9.3-p448

This step will download, extract, configure and compile so it may take some time depending on your CPU and connection and if something goes wrong during one of these steps, be calm and read carefully the messages given, they are really helpful.

When Ruby is installed you can check it:

ruby -v

Which will return, in case you installed 1.9.3-p488, something like this:

ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]

You can install any Ruby version you wish following the above step and to use it you do this:

rvm use 1.9.3

Let's suppose you also have Ruby 2.0.0 installed, to use it:

rvm use 2.0.0

You can also reinstall it. That will happen in case your OS doesn't have a required library, e.g.: libyaml and you have already installed Ruby

rvm reinstall RUBY_VERSION

And voilà

That's it about installing Ruby.

Now let's work with Gemsets. This is how you'll organize all your Gems so you don't make a mess.

First you need to check all the available Gemsets

rvm gemset list

It will list a (default) and global Gemsets. If you do not specify the Gemset where you want to install a gem, it will be installed in the default Gemset.

The global Gemset has some Gems that are used frequently like bundler, json, rake, rdoc, etc, to have a complete list use gem list.

Now you'll create a Gemset called my_project and we'll install rails 3.2.14

rvm gemset create my_project

You can check it using rvm gemset list

IMPORTANT: For each and every Ruby version there will be a different Gemset list. To test it, change your Ruby version (rvm use 1.9.3 or whatever) and check it's Gemset list.

To use that Gemset

rvm use 1.9.3@my_project

Now install your gems

 gem install rails -v 3.2.14  

When the process is over, you can check it using

rails -v

And that Rails version will be available only when using that Gemset.

Obviously you can create lots and lots of Gemsets and each with lots and lots of Gems that will not conflict with each other.

Now let's suppose you'll create a project using Rails 4.0.0

You'll create a new Gemset for that project

rvm gemset create my_new_project

rvm use 1.9.3@my_new_project

gem install rails

Done, you have now two Gemsets (my_project and my_new_project) each with a different Rails version and you can install all other Gems required for your project.

If you need to remove a Gemset, you must be using it (rvm RUBY_VERSION@GEMSET_NAME) and do this:

rvm gemset delete

You can rename your Gemset:

rvm gemset rename current_name new_name

To make my life easier I have created some aliases on my .bash_profile so I don't have to type rvm RUBY_VERSION@GEMSET_NAME.



That's it, if you know a way I can improve it, please let me know it.

I wish you the best.

Today, be calm when you would be nervous

Sunday, September 8, 2013

Making spree 2.0.3 show your gateway error messages

Hi there, happy?

I have developed an active merchant gateway, called 2Pay and I configured my Spree to use it.

It was working great until I got warned that when something goes wrong with the client's credit card, spree is always showing a default error message.

Well, that is terrible because the client will have no idea of what is going on among all possibilities.

PAUSE: I have this on StackOverflow if you feel more comfortable there :)

I had to do one thing, check the spree_core source code using

bundle open spree_core
So I noticed how the checkout_controller was calling a default message when something goes wrong with the Gateway object so I had to update it but it didn't work 100%.

Searching a bit more I found that the order.rb model rescue block wasn't adding the error to the errors hash.

Anyway, these are the two files you need to override and to do that you need to create checkout_controller.rb as checkout_controller_decorator.rb on /controller and the order.rb as order_decorator.rb on /model.

I know the paths are obvious but some user on #spree channel told me to save them on different folder and that was making me more confused.



I really hope it helps you my friend.

Now, be a nice person and say something nice to who's with you :)

Tuesday, February 19, 2013

Rails errors with Foundation style with simple_form

I just made my error messages use Foundation styles  (http://foundation.zurb.com/docs/forms.php, scroll until Error States)

I edited the simple_form initializer:



Now you have cool error/alert/whatever messages.

Be happy

Friday, February 15, 2013

RSpec or UnitTest

I asked that myself 5 minutes ago and wasted 3 searching and guess what, 50% votes for RSpec and the other half to UnitTest. So, based on this "in depth" research, I came up with the solution.

I doesn't matter at all, choose that one that reads cleaner. It's like asking: "What is the best editor?"

Tuesday, February 12, 2013

Foundation fonts not working

I just made my application work with Foundation Fonts. It is simple but not obvious if you follow the step-by-step.

Acabei de fazer minha aplicação usar as fontes do , é bem simples mas não tão óbvio se você seguir o

They say it is simple like adding a bit of code, of course you need to make some changes.



I won't work.

I made some tests, read some stackoverflow answers and nothing...

Just to make sure the problem wasn't with assets pipeline I printed this in my console:

p Rails.application.config.assets.paths

Guess what. My fonts path was there.

/app/assets/fonts so I was sure there problem was my CSS

That's when I checked the downloaded file from Zurb and the one from the page and notice the difference.



See? There are some classes references to each icon so it's not just adding those i tags and you're done.

In case you need a CSS with all fonts, I have a gist for you

Be happy!

Monday, January 28, 2013

Testing different database connections

Hello world!

I had to test some database connections from inside my application.

The user fill a form with the required information but before saving it to my PostgreSQL instance I had to check if everything is OK.

I was trying to use my model like this:


And guess what, as expected it replaces my application connection.

The solution is pretty simple, use the adapter class like:


The reason for this code be inside a bedin/rescue is that if anything is wrong with the connection data it will return 500 Internal Server Error

Problem solved.

Monday, October 22, 2012

Ruby - Installing gem eventmachine

Hi fellas!

Trying this:

$ gem install eventmachine


Returned me this:

make
compiling ssl.cpp
make: g++: Command not found
make: *** [ssl.o] Error 127

The solution is installing the build-essential package:

$ sudo apt-get install build-essential


Problem solved!

Friday, October 19, 2012

Ruby - Installing gem debugger

Hi fellas!

Tryin this:


$ gem install debugger


Returned this:


checking for vm_core.h... no
checking for vm_core.h... no
Makefile creation failed
**************************************************************************
No source for ruby-1.9.3-p286 provided with debugger-ruby_core_source gem.
**************************************************************************



Reading the error I tried this:



$ gem install debugger -- --with-ruby-include=/home/$(echo $USER)/.rvm/src/ruby-$(ruby -v | cut -d" " -f2)

Problem solved.

Adios!

Thursday, October 11, 2012

Rails - Can't convert fixnum to string during rake db:create

Hi fellas!

I'm using MySQL and tried to run rake db:create and this error was returned:

`connect': can't convert Fixnum into String (TypeError)


The problem is that YAML parser is trying to guess what data type value I have in my datase.yml.

My file was like this:

development:
adapter: mysql2
encoding: utf8
reconnect: false
database: walk_benchmark_development
pool: 5
username: root
password: 123
socket: /var/run/mysqld/mysqld.sock


So the parser probably considers my password as a integer and forcing it to interpret my password as a string works.

password: "123"