Before calling the ajax request, add this:

$.ajaxSetup({
‘beforeSend’: function(xhr) { xhr.setRequestHeader(‘X-CSRF-Token’, $(“meta[name='csrf-token']“).attr(‘content’)); }
});
then you can all the ajax reqeust after that:
$.ajax({
type: “POST”,
url: “/your-url-here”,
data:  $(form).serializeArray()
});
Make sure to add this in your view template:
<%= csrf_meta_tag %>
And add this javascript :
https://github.com/rails/jquery-ujs
Enjoy!
 

On OSX, I’ve installed  RVM, ruby 1.9.2, mysql2 gem, Rails 3.1.0. When starting rails server, I’m getting the following errors:

`require’: dlopen(/Users/mike/myapp/ruby/1.9.2/gems/mysql2-0.3.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError)
This simply solved my problem:

$ export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

Source: http://rorguide.blogspot.com/2011/07/getting-error-library-not-loaded.html

 

Following on from previous post on installing MongoDB 1.8.1, here are similar steps to getting Redis 2.2.4 running on Ubuntu 10.10 using an init script. The setup is intended to be used on developer desktop/laptop rather than production infrastructure.

As ever, first download and unzip Redis from here.

cd /tmp
wget http://redis.googlecode.com/files/redis-2.2.4.tar.gz
tar -zxf redis-2.2.4.tar.gz
cd redis-2.2.4
make
sudo make install

Your Redis binaries should now be located in /usr/local/bin.

To get an init script and Redis config working cleanly with this setup, download my init and config files from my Github ‘dotfiles’ repo. My init script is pretty standard. However my redis.conf sets Redis up with 1Gb of virtual memory and 20Gb of swap space – intended for general development purposes.

wget https://github.com/ijonas/dotfiles/raw/master/etc/init.d/redis-server
wget https://github.com/ijonas/dotfiles/raw/master/etc/redis.conf
sudo mv redis-server /etc/init.d/redis-server
sudo chmod +x /etc/init.d/redis-server
sudo mv redis.conf /etc/redis.conf

Before you can fire up the Redis server for the first time, you’ll need add a redis user and prep a data and logging folder.

sudo useradd redis
sudo mkdir -p /var/lib/redis
sudo mkdir -p /var/log/redis
sudo chown redis.redis /var/lib/redis
sudo chown redis.redis /var/log/redis

Also, you need to activate your Redis services init script by adding it to your system’s run-level configuration. That way the service will startup during the boot sequence and stop nicely during the OS’ shutdown procedure.

sudo update-rc.d redis-server defaults

You’re now ready to launch Redis server with

sudo /etc/init.d/redis-server start

Good luck!

Source: http://www.denofubiquity.com/nosql/412/

 

jQuery with Rails

On September 26, 2011, in Ruby on Rails, by admin

If you encounter same problem i have mine where :confirm does not work in:

<%= link_to “Delete”,  location, ;confirm => “Are you sure?” %>

Solution:

1) Grab the jQuery driver at http://github.com/rails/jquery-ujs and put it in your javascripts directory. The file is at src/rails.js

2) Put these codes to your layout file:

= javascript_include_tag “http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js”
= javascript_include_tag ‘rails’

3) to authenticate ajax request from CSRF attack add this to your header :

<%= csrf_meta_tag %>

More information at: http://joshhuckabee.com/jquery-rails-3

Enjoy! :)

 

I have encountered this just recently and this just solved the problem. Add the codes below to your application.js

function CSRFProtection(xhr) {

var token = $(‘meta[name="csrf-token"]‘).attr(‘content’);
if (token) xhr.setRequestHeader(‘X-CSRF-Token’, token);
}
if (‘ajaxPrefilter’ in $) $.ajaxPrefilter(function(options, originalOptions, xhr) { CSRFProtection(xhr); });
else $(document).ajaxSend(function(e, xhr) { CSRFProtection(xhr); });

function CSRFProtection(xhr) {   var token = $(‘meta[name="csrf-token"]‘).attr(‘content’);    if (token) xhr.setRequestHeader(‘X-CSRF-Token’, token);}if (‘ajaxPrefilter’ in $) $.ajaxPrefilter(function(options, originalOptions, xhr) { CSRFProtection(xhr); });else $(document).ajaxSend(function(e, xhr) { CSRFProtection(xhr); });

Hope this helps :)

 

Installing RMagick on Ubuntu

On September 18, 2011, in IT Related, Ruby on Rails, by admin
$ sudo aptitude install -y imagemagick
$ sudo aptitude install -y libmagick9-dev
$ sudo gem install rmagick
 

Mysql problem when bundling

On September 6, 2011, in Ruby on Rails, Ubuntu, by admin

Problem encountered:

Installing mysql2 (0.2.13) with native extensions /home/mike/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:552:in `rescue in block in build_extensions’: ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

/home/mike/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb
checking for rb_thread_blocking_region()… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lm… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lz… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lsocket… no
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lnsl… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lmygcc… no
checking for mysql_query() in -lmysqlclient… no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Solution:

sudo apt-get install libmysqlclient15-dev

Then bundle install again and it should work.! :)

 

Textmate for Ubuntu Linux

On July 27, 2011, in IT Related, Ubuntu, by admin

Transforming gEdit to Textmate in 4 simple steps.

Please follow instructions in this link:

http://blog.sudobits.com/2011/04/02/textmate-for-ubuntu-linux/

 

This validation does not work in IE 6 and 7. Because IE treats files differently,  they added extra text on them (I’ve not clear on this point why they add).

please refer this link http://groups.google.com/group/paperclip-plugin/browse_thread/thread/3e09d6943616ce7b

Solution For This problem:
you need to add following format in your validation

for jpeg image - image/pjepg and for png image image/x-png format add in your in validates_attachment_content_type

eg: validates_attachment_content_type :image, :message => ‘Please upload correct format’, :content_type => %w( image/jpeg image/png image/gif image/pjpeg image/x-png )

For ‘gif’ image no additional format require in IE.

Source: blog.joshsoftware.com

 

http://articles.slicehost.com/2009/3/13/ubuntu-intrepid-nginx-rails-and-thin

Having installed the thin web server for Ruby, we can now look at configuring an Nginx vhost to proxy to thin so we can serve our Ruby on Rails application.

The process is easy to follow and easy to repeat for hosting multiple domains.


Setup

First, you need to have followed the previous Nginx articles, having installed Nginx via aptitude or via source.

You will also need to have installed the thin web server — see the article linked above.

Plan

The plan is very simple:

We’ll create a basic rails application and use 3 thin instances running from port 3000 to 3002.

I won’t go into detail as the thin web server for Ruby article shows how to install and configure thin.

Once that is done, we’ll create a simple vhost to proxy requests to the thin instances.

You may also notice this article is very similar to the Nginx and mongrels article. There is a very good reason for that — they use exactly the same methods in creating the virtual host.

All the virtual host has to do is proxy requests to the 3rd party web server — in this case thin, in the sister article, mongrels.

Rails Application

To create a rail application, move into your public_html folder:

cd /home/demo/public_html

and create a new Ruby on Rails application:

rails railsapp

Done.

Thin

Ensure you are in the rails folder:

cd ~/public_html/railsapp

Then create a thin configuration file:

sudo thin config -C /etc/thin/railsapp.yml -c /home/demo/public_html/railsapp/  --servers 3 -e production

It’s always a good idea to check the created file:

cat /etc/thin/railsapp.yml

The contents are as such:

---
pid: tmp/pids/thin.pid
address: 0.0.0.0
timeout: 30
port: 3000
log: log/thin.log
max_conns: 1024
require: []

environment: production
max_persistent_conns: 512
servers: 3
daemonize: true
chdir: /home/demo/public_html/railsapp

Unlike working with mongrels, with thin we don’t need to manually create symlinks, etc. to make sure it’s started after a reboot.

Now all we need to do is start thin:

sudo /etc/init.d/thin start

Done.

Nginx Virtual Host

Let’s create the Nginx vhost:

sudo nano /etc/nginx/sites-available/domain.com

Note: If you installed Nginx from source, the path may vary to something like:

sudo nano /usr/local/nginx/sites-available/domain.com

The contents of the file are as such:

upstream domain1 {
        server 127.0.0.1:3000;
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
    }

server {
            listen   80;
            server_name  www.domain.com;
            rewrite ^/(.*) http://domain.com/$1 permanent;
           }

server {
            listen   80;
            server_name domain.com;

            access_log /home/demo/public_html/railsapp/log/access.log;
            error_log /home/demo/public_html/railsapp/log/error.log;

            root   /home/demo/public_html/railsapp/public/;
            index  index.html;

            location / {
                          proxy_set_header  X-Real-IP  $remote_addr;
                          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                          proxy_set_header Host $http_host;
                          proxy_redirect false;

                          if (-f $request_filename/index.html) {
                                           rewrite (.*) $1/index.html break;
                          }

                          if (-f $request_filename.html) {
                                           rewrite (.*) $1.html break;
                          }

                          if (!-f $request_filename) {
                                           proxy_pass http://domain1;
                                           break;
                          }
            }

}

Take it section by section and you will see that the basics are the same as for a ‘normal’ Nginx vhost.

We have the server_name, listen, log and index variables.

Where it differs is the addition of the Rails proxy settings.

In this example, we will use 3 thin instances running on ports 3000, 3001 and 3002 which I have defined in the ‘upstream’ setting. I’ve called it domain1 for ease of use.

The location settings say that if the requested file exists to serve the static version straight away.

And if the requested file doesn’t exist, pass the request to the thin server.

Enable

Remember that we need to ‘enable’ any available vhosts or it won’t be served (an easy thing to leave out).

Referring to the Nginx articles, all we need to do is create a simple symlink:

sudo ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/domain.com

Or, if you installed Nginx from source:

sudo ln -s /usr/local/nginx/sites-available/domain.com /usr/local/nginx/sites-enabled/domain.com

Done.

Restart

Final step is to restart Nginx:

sudo /etc/init.d/nginx stop
...
sudo /etc/init.d/nginx start

Use the ‘stop’ and ‘start’ method as issuing a restart command does not always work with Nginx.

Navigate

All that’s left is to navigate to your domain:

http://www.domain.com

Where you will be greeted with the rails welcome page.

Summary

Setting up an Nginx virtual host to proxy to a thin server cluster is easy.

To serve multiple domains, simply repeat the process with the new domain details.