Posts

Showing posts from 2013

Set up Jruby with apache and passenger in EC2

How to set up jruby with apache and passenger in amazone ec2. Step 1: First install jruby using please check the current version in the   link . And make sure you are installing the bin version of it wget 'http://jruby.org.s3.amazonaws.com/downloads/1.7.4/jruby-bin-1.7.4.tar.gz' Step 2: extract the tar file using tar zxvf jruby-bin-1.7.4.tar.gz Step 3: We need to set come environment variable so that it will be accessible from every other place. export JRUBY_HOME=/home/home_directory_name/jruby-1.7.4 export PATH=$JRUBY_HOME/bin:$PATH And Open the .bashrc file and put the path in their sudo nano ~/.bashrc PATH=$PATH:/home/ubuntu/jruby-1.7.4/bin Step 4: Now to check if jruby is properly installed or not put jruby-v If your system does not have java installed then it will show an error like /home/ubuntu/jruby-1.7.4/bin/jruby: line 395: exec: java: not found Don't worry we will install java to fix this. sudo apt-get install openjdk-7-jdk apt-cache

Set up ruby 2.0 and Rails 4.0.1 in Rackspace

First we need to install ruby Step 1)Install Ruby apt-get -y update apt-get -y install build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev cd /tmp wget http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz tar -xvzf ruby-2.0.0-p247.tar.gz cd ruby-2.0.0-p247/ ./configure --prefix=/usr/local make make install Step 2) Install Rails gem install rails 4.0.1 Step 3) Install passenger gem install passenger Step 4) Install Apache2 apt-get install apache2 Step 5) Install Mysql sudo apt-get install mysql-server mysql-client That's it we are done with the setup.

Active resource in rails

This is a powerful feature of rails to communicate between two models in two different application running in two different server. ActiveResource:Base is the main class for mapping RESTful resources as models in a Rails application. Active Resource objects represent your RESTful resources as manipulatable Ruby objects. To map resources to Ruby objects, Active Resource  only needs a class name that corresponds to the resource name (e.g., the class User maps to the resources Products, very similarly to Active Record) and a site value, which holds the URI of the resources. class User < ActiveResource::Base   self.site = "http://api.products.com:3000/" end In order to access products.com controller you need to send the json response like i am going to call the index action so we need to write :- def index @products = Product.all respond_to do |format| format.json { render :json => @products} end end Now the User class is mapped to RESTful resources located

How Action Mailer Works in Rails

Sending emails in a rails application is very easy and quite handy using action mailer. First we will go through the basic setup of action mailer then we will go in details how it works. Step 1   We can setup a mailer class using rails generator   rails generate mailer UserMailer Step 2   Then we can create any instance method inside it.     def welcome_email(user)       @user = user       @url  = 'http://example.com/login'       mail(to: @user.email, subject: 'Welcome')     end Step 3   We can call this method form model or controller or any kind of callbacks using    Like     UserMailer.welcome_email(@user).deliver Step 4   You can set up a view template for the welcome_email in side the views/mailer directory   Like   welcome_email.html.erb   and you can specify the layout format here and can also access the instance variables defined in the       welcome_email(user) method Step 5   Additionally we can set up the smtp configuration inside

Interesting ubuntu trick

1) tail cannot watch no space left on device.    Solutions   a)  echo 900000 | sudo tee /proc/sys/fs/inotify/max_user_watches      Or Place   b) "fs.inotify.max_user_watches=16384" in /etc/sysctl.conf 2)  How to remove all files and sub-directories in a directory WITHOUT deleting the directory in bash? Solutions rm -rfv dontDeleteMe/*

How to upload Bulk file in rails 3 application

If you want to upload bulk file in rails application just follow the following steps 1) create a rails application     rails new bulk_upload 2) Add passenger in your gem file     gem 'passenger' Now try to start with your rails application with the command   passenger start by default it will run on port number 3000 Now if you try to upload bulk files lets just say more than 500 mb or 800 mb you will get an error in the time of uploading like [ error] 4840#0: *557 client intended to send too large body: 94672000 bytes, client: 127.0.0.1, server: _, request: "POST /XXX HTTP/1.1", host: "localhost:3000" so this not error of you application this is the error thrown by passenger and ngnix which is responsible to run passenger First uninstall using purge to remove even configuration files and records:   apt-get purge nginx nginx-common nginx-full Then reinstall it  apt-get install nginx If above doesn't work for you you can also

upgrade to rails 4 and ruby 2

To upgrade your rails version to rails 4 is pretty easy just follow the below steps. - I assume you have already rails installed in your system and also you are using rvm and ruby. - If you are not using rvm i recomend you to use rvm. please check the link for rvm ( http://rvm.io/ ) open a terminal and follow the steps. Step 1:  rvm get head (to get the latest version of rvm) Step 2:  rvm install 2.0.0 (to install ruby) Step 3:  rvm use 2.0.0 Step 4:  rvm gemset create rails4 Step 5:  gem install rails That's it to upgrade your rails version to rails 4 and ruby 2 in ubantu 12.04. Now if you run rails new new_app it will automatically create your rails application in rails 4 simple :) Further if you want to use both rails 3 and and rails 4 you can create a .rvmrc file in your application and mention the gem set in that file. echo "rvm ruby-1.8.7-p352@gemset" > .rvmrc And inside the file mentioned your gem set. like rvm use 2.0.0 rvm gem

sunspot_rails the Sunspot library for Solr search.

Now its very easy to integrate solar search using sunspot_rails gem in rails 3 for fast searching.  Please follow the below steps to use solar search. 1) first install the gem    gem 'sunspot_rails'    gem 'sunspot_solr'   # optional pre-packaged Solr distribution for use in development 2) Run the bundle command to install the gems. 3) Run      rails generate sunspot_rails:install      to create the configuration file for the solar search. 4) Now you need to start the solar server     bundle exec rake sunspot:solr:start 5) If you have some exiting data in the data base please execute the command     rake sunspot:reindex 6) Now we are done with our setup.we need to configure our model so that search will work.    let say we have one event model in our application like    class Event < ActiveRecord::Base       attr_accessible  :content, :title     end Now if we want a full text search on the Event model. We just need to add one block in t

mongo.js:L112 Error: couldn't connect to server 127.0.0.1:27017 atsrc/mongo/shell/mongo.js:L112

mongo.js:L112 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112 Stop mongodb service sudo service mongodb stop Remove mongodb lock file sudo rm /var/lib/mongodb/mongod.lock Change ownership from root to mongodb path sudo chown -R mongodb:mongodb /var/lib/mongodb/ Start mongodb service sudo service mongodb start Test mongo app mongo Then you will be able to execute successfully (i hope).

Configure Twilio In rails 3

Now you can make voice call and can send text message using Twilio api in rails 3. Follow the below steps for setting up Twilio in your Rails application. 1) First create a new rails application using rails new twilio_app . 2) Add the gem "twilio-ruby" in your gem file and run the bundle command. 3) Go to http://www.twilio.com/ and create a free new account. After creating the account just go to the https://www.twilio.com/user/account   and get the ACCOUNT SID and AUTH TOKEN from there . 4 )Now for sending sms put the code inside your controller.    account_sid =  ACCOUNT SID    auth_token =  AUTH TOKEN    @client = Twilio:: REST ::Client. new account_sid, auth_token    message = @client .account.sms.messages.create( :body => "Hey Jhon !" ,              :to => "+14159352345" ,              :from => "+14158141829" )    puts message.sid for trial account to  number must be registered inside tw

Delete and reset database in rails 3

Suppose I have a Ruby on Rails database . I want to delete everything and rebuild the database. Please follow the following steps: rake db:drop rake db:create rake db:migrate  To reset and reload your current schema with all. Just follow rake db:reset rake db:migrate

Rmagic installation problem in rails 3

How to install ImageMagick  In Rails 3 Before installing r magic please installed the required library in your system. please follow the Steps. sudo apt-get install imagemagic sudo apt-get install libmagickcore-dev sudo apt-get install l ibmagickwand-dev After successful installation of the above 3 libraries now run gem install 'rmagick' Now rmagic will install successfully. :)

How to write stored procedure in rails 3

Writing Stored procedure in rails 3 Now using Mysql2 gem writing stored procedure is pretty much easy. Just we need to follow the below steps to write  stored procedure in ORM. 1) Connect the database. client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "XXX", :password => "XYZ", :flags => Mysql2::Client::MULTI_STATEMENTS ) This extra flag option is most important and multiple result sets is normally used when calling stored procedures that return more than one result set. 2) Fetch the data. result  = client.query( 'CALL sp_test'); It will return the Result set. Now result contains first result set and to fetch the Next result set we need to follow the next step While (client.next_result)    result = client.store_result  #To get the next result set end

Rails 3 ActionDispatch::Cookies::CookieOverflow

ActionDispatch :: Cookies :: CookieOverflow ( ActionDispatch :: Cookies :: CookieOverflow ): Problem in rails 3 devise. The problem is with session["devise.facebook_data"] = env["omniauth.auth"] . Twitter's response contains an extra section that is very large and does not fit in the session. One option is to store env["omniauth.auth"].except("extra") in the session instead. This is issue arises where we are storing more than 4K of session data.