Tuesday, April 3, 2012

Drag and Drop / Change Position / Move with JQuery and Rails

There are various ways to drag and drop or move or change positions of items.
Here I am listing 2 scenarios using Query.

Lets start it with the following basic steps:
1) Download Download jQuery (version 1.2 or above), then the TableDnD plugin from GitHub (current version 0.6).
2) Reference both scripts in your HTML page in the normal way.
3) Initialize the tables is in the $(document).ready function. Use a selector to select your table and then call tableDnD().

First scenario: (Using JQuery )
After following above steps HTML page will look like:

<%= javascript_include_tag 'jquery.js', 'jquery.tablednd.0.6.min.js', 'jquery.tablednd.js' %>
<h1>Listing Softwares</h1>
<div class="createHeaderApp"> <h3>Softwares...</h3> </div>
<div class="titleWide"></div>
 <div class="jobsHeader">
    <div class='clearfloat'></div>
    <div style="width:595px; float:left;">Name</div>
    <div style="width:70px; float:left;">Show</div>
    <div style="width:70px; float:left;">Edit</div>
    <div style="width:70px; float:left;">Delete</div>
    <div class='clearfloat'></div>
</div>
<table id="softwares">
  <tbody class="appsHeader1" style="overflow:hidden;">
    <% @softwares.each do |software| %>
     <tr class="<%= cycle("even", "odd") -%>" style="font-size:12px; width:875px;  "id="soft-<%= software.id %>">
       <td style="width:595px; float:left;"><%= software.name %></td>
       <td style="width:70px; float:left;"><%= link_to 'Show', software %></td>
       <td style="width:70px; float:left;"><%= link_to 'Edit',  edit_software_path(software) %></td>
      <td style="width:70px; float:left;"><%= link_to 'Delete', software, :confirm => 'Are you sure?', :method => :delete %></td>
      <td class='clearfloat'></td>
    </tr>
  <% end %>
</tbody>
</table>

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
    // Initialise the table
    $("#softwares").tableDnD();
});
</script>

Now run server, execute application and test it.(Using above code you can now move or change position of list items with internal links or data.)

Second scenario:(Using JQuery & AJAX function )
If you want to save/update the positions of moving items in the database then you have to call AJAX function like below:

1)Generate migration to add new integer field "position" into softwares table
   rails g migration add_position_to_softwares

2)Change $(document).ready function like below:
<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
      $('#softwares').tableDnD({
        onDrop: function(table, row) {
          $.ajax({
             type: "POST",
             url: "<%= url_for(:action => 'sort') %>",
             processData: false,
             data: $.tableDnD.serialize() + '&authenticity_token=' + encodeURIComponent('<%= form_authenticity_token if protect_against_forgery? %>'),
             success: function(msg) {
               alert("The specifications have been updated")
             }
           });
        }
      })
    })
</script>

3)Change routes.rb abd add new action into your route file.
   match "/sort" => "softwares#sort"

4)Add new definition "sort" into specified controller, for me its  softwares_controller.rb
 def sort
    Software.all.each do |soft|
      if position = params[:softwares].index(soft.id.to_s)
        soft.update_attribute(:position, position + 1) unless soft.position ==  position + 1
      end
    end
    render :nothing => true, :status => 200
  end

The controller iterates over all the softwares, checking the position in the db (see the acts_as_list plugin) versus the position in the array that was sent in the request. For the items that are affected, it updates the position in the db. Since we are only calling this action via AJAX, we just render nothing and indicate a successful status.
 
5)Now restart server and test application.

Thursday, March 1, 2012

Setup Refinery CMS with rails 3.2.2

Refinery prerequisites :
Ruby – 1.8.7, 1.9.2, Rubinius, and JRuby are all acceptable
RubyGems – Recommended that you have the latest version installed
Database – SQLite3 (default), MySQL, or PostgreSQL
ImageMagick – Recommended that you have the latest version installed

If you already have prerequisites then proceed further steps:
1) Install the Gem 

gem install refinerycms
2) Generate an Application
refinerycms path/to/my_new_app

3)Do you have devise.rb file in your project ? If no then
rails g refinery:cms (it should copy devise.rb file in initializer)
4)Start up your site
cd path/to/my_new_app/ 

rails server

Now visit http://localhost:3000 and you should see your Refinery CMS site and you will be prompted to setup your first user. That's all it takes to install and run your Refinery CMS site! 
 

Monday, February 27, 2012

Execute rake file in crontab(RVM)

While executing rake in crontab using rvm, needs to load it properly.

For every 1minute.
 */1 * * * * cd /home/user/application_path && /home/user/.rvm/bin/rvm use ruby-1.9.2-p136 rake reminder_email >> /home/user/crontab_errors.txt


For dayily
0 0 * * * cd /home/user/application_path && /home/user/.rvm/bin/rvm use ruby-1.9.2-p136 rake reminder_email >> /home/user/crontab_errors.txt

Thursday, February 16, 2012

Paperclip 'identify' command error on ubuntu.

The Paperclip.options[:command_path] setting is for the location of your ImageMagick executables (in this case identify).

Try running which identify and setting the option to be the directory that is returned like identify is hashed (/usr/bin/identify).
If that command doesn't return anything, make sure that ImageMagick is properly installed.

If not installed then run following command and install it.

sudo apt-get install imagemagick
sudo apt-get install libmagickwand-dev
gem install rmagick

Then set Paperclip.options[:command_path] = "/usr/bin" in development.rb file

Problem solved.

Friday, December 16, 2011

How to resolve jcode (LoadError) with contacts gem in rails 3?

Ruby >= 1.9 doesn't have jcode, a module to handle japanese (EUC/SJIS) strings, as it supports unicode natively.

So you will need to add: require 'jcode' if RUBY_VERSION < '1.9' to your gdata gem found under your .rvm directory somewhere similar to this:

/home/.rvm/gems/ruby-1.9.2-p0@your_gemset_name/gems/gdata-1.1.1/lib/gdata.rb
change line 21 to:
if RUBY_VERSION < '1.9'
  require 'jcode'
  $KCODE = 'UTF8'
end

Thursday, December 8, 2011

Export data to XLS/CSV/TEXT from MySQL

To dump all the records from a table called "users" into the file /home/sapna/users.xls as a XLS file you need to do is.

1) Go to mysql comand prompt using command
mysql -u root -p database_name

2)Then use the following SQL query:
SELECT *
INTO OUTFILE '/home/sapna/users.xls'
FIELDS TERMINATED BY ','
ENCLOSED BY ' " '
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM users;
Note that the directory must be writable by the MySQL database server. 
If it's not, you'll get an error message like this:
Can't create/write to file '/home/sapna/users.xls' (Errcode: 13)
 
To resolve above error, you need to do is
i)sudo gedit /etc/apparmor.d/usr.sbin.mysqld
ii)Add line at last  /home/sapna/* rw, - means read/write permission of folder where you want to write xls file
iii)And then make AppArmor reload the users. 
sudo /etc/init.d/apparmor restart/reload
Also note that it will not overwrite the file if it already exists, 
instead showing this error message:
File '/home/sapna/users.xls' already exists
 

Thursday, December 1, 2011

NGINX server configurations.

1) To find path of nginx.conf file.
locate nginx.conf
It will give you locaiton of nginx file.
/usr/local/nginx/conf/nginx.conf                                                                                       
/usr/local/nginx/conf/nginx.conf.default

2)To set domain name
Edit nginx.conf.default
nano /usr/local/nginx/conf/nginx.conf.default
Change server_name settings

server {
                listen       80;
                server_name  localhost; #instead of localhost set live domain name like abc.com

                access_log  logs/localhost.access.log  main;

                location / {
                    root   html;
                    index  index.html index.htm;
                }
        }
}

3)After making of any changes in nginx.conf file you need to reload it.
/etc/init.d/nginx reload

4)After making any changes of project files on server you need to restart nginx server.
/etc/init.d/nginx restart

5)To kill process of nginx
ps aux | egrep '(PID|nginx)'
and kill the PID

Refer site:http://library.linode.com/web-servers/nginx/configuration/basic