Monday, September 28, 2009

Connect to database using ruby and ActiveRecord

To get connected you database from your ruby file just include the "ActiveRecord" inside your ruby file.

require 'rubygems'
require 'activerecord'

#establish the connection.
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:database => "database",
:username => "username",
:password => "password",
:host => "localhost"
)

#create a class inherit with you activerecord base class.
class User < ActiveRecord::Base
end

user = User.find(:first)
puts user
Or

class SomeThing < ActiveRecord::Base
set_table_name :users
end

user = SomeThing.find(:first)
puts user

That's It.

Ruby Mysql database connectivity

In this section I am going to tell you how do you get connected your MySQL database using Ruby.

Make sure that you have install mysql adapter
To verify you mysql file go to command prompt type irb -> require 'mysql' if it's return true that means the file is loaded.

Now use your fav editor let say scite.

require 'rubygems'
require 'mysql'

begin
#establish a connection.
mysql = Mysql.real_connect("hostname(e.g - localhost)", "username", "password", "databasename")
result = mysql.query("SELECT * FROM USERS;")
result.each do |row|
row.each do |col|
puts col
end
end
#the col object will return the value from the users table.
# if you want to insert a record. then
mysql.query("INSERT INTO users (email) VALUES ('example@example.com')")
#this is how you can edit delete the records...
rescue Exception => e
puts "I guess something went wrong. #{e}"
ensure
# clone the connection...
mysql.close if mysql
end

Friday, September 11, 2009

bash: cap: command not found

bash: cap: command not found

In this post I'll be talking about how to resolve the “bash: cap: command not found” issue. Even after installing Capistrano we get the following error “bash: cap: command not found” the reason behind this is that the path is not decalred in bash file, so to fix the problem you have to add the following line on code inside bashrc file

Step 1
#go to Terminal
#type
sudo gedit ~/.bashrc

Step 2
#Add the following line at the bottom of the file.
export PATH=$PATH:/home/user/.gem/ruby/1.8
#save and close
# To find the installation of you gem just run the following command
#gem list -d c
# capistrano (2.5.8)
# Authors: Jamis Buck, Lee Hambley
# Rubyforge: http://rubyforge.org/projects/capistrano
# Homepage: http://www.capify.org
# Installed at: /home/user/.gem/ruby/1.8

Step 3
#From command prompt
source ~/.bashrc

Wednesday, September 9, 2009

File Upload using AJAX and jQuery.

File Upload using AJAX and jQuery.

File upload using jQuery is preety simple.

NOTE: please include the jQuery Library afer prototype.js

As you can in the above example I had declared “jQuery.noConflict();” the reason behind this is to abvoid the conflict between jQuery library and prototype.js.

<% remote_form_for :some_random_name, @some_object, :url => {:action => 'testtest'},
:html => { :multipart => true, :id => "upload_form" } do |f| %>


<%= file_field_tag "test" "uploaded" %>
< id="button" class="button" value="Add File" type="button">
< id="save" class="save" value="Upload" type="submit">
<%end%>

Thats it.

Monday, May 25, 2009

How to integrate Comatose CMS in your rails application.

Comatose CMS Yet another Micro CMS, when your client may use to create and edit easily a static pages like privacy policy, terms & conditions, an FAQ, that kind of thing.

Installation of Camotose.
$ ./script/plugin install git://github.com/darthapo/comatose.git
$ ./script/generate comatose_migration
$ rake db:migrate

Once the above mentioned command is executed then configure your route file, at the bottom of your route file paste the below mention syntax
map.comatose_admin
map.comatose_root ''

That it now you to access the Camotose simply type
http://localhost:3000/comatose_admin

You can configure you comatose on your config/environment.rb file, but i'll suggest you to create a comatose.rb inside your config/initializers folder, and insert the following code.

Comatose.configure do |config|
config.admin_title = 'YOUR site name CMS SYSTEM' # it cab anything...
config.admin_helpers = []
config.admin_sub_title = 'Administration Pages'
config.content_type = 'utf-8'
config.default_filter = ''
#config.default_processor = :liquid
config.default_tree_level = 3
config.disable_caching = false
config.hidden_meta_fields = 'filter'
config.helpers = []
config.includes = []
# These are 'blockable' settings
config.authorization = Proc.new { true }
config.admin_get_author = Proc.new { request.env['REMOTE_ADDR'] }
config.after_setup = Proc.new { true }

# Includes AuthenticationSystem in the ComatoseAdminController
config.admin_includes << :authenticated_system

# Calls :login_required as a before_filter
config.admin_authorization = :login_required

end

Wednesday, December 24, 2008

Insert HTML Tag Using Javascript...

The below mention function will be use to insert a div tag along with input field, button when a user clicks on hyper link. Button will be used to hide the selected div. A user can create max 5 div.


function attachFile(id)
{
var count = new Array("1","2","3","4","5")
var hiddenCount = document.getElementById("Cnt").value;
var Comman = 1;


for(var j=1;j<=count.length;j++)
{
if (id == 'remove_' + j)
{
document.getElementById('div_' + j).style.display = "none";
return
}
}
Comman = parseInt(hiddenCount) + Comman;
if (Comman <= 5)
{
var varTag1 = document.createElement("INPUT");
// var varTagRemove = newElement("input",{type:"button",name:"Remove",id:"remove_"+Comman,value:"1"},{onclick:attachFile(this.id)});
var varTagRemove = document.createElement("INPUT");
varTagRemove.type="button";
varTagRemove.value = "Remove";
varTagRemove.id = "remove_"+Comman;
varTag1.type = "file";
varTag1.id = "file_"+Comman;;
var x = document.all;
var br = "br"
if(x)
br = "
"
var varTag2 = document.createElement(br);

document.getElementById("div_" + Comman).appendChild(varTag1);
document.getElementById("div_" + Comman).appendChild(varTagRemove);
document.getElementById("div_" + Comman).appendChild(varTag2);
var rm_test = document.getElementById("remove_" + Comman);
rm_test.onclick = function(){attachFile(this.id)};
document.getElementById('Cnt').value = Comman;

}
else
{
for (var i = 1; i <= count.length; i++)
{
if (document.getElementById("div_" + i).style.display == "none")
{
document.getElementById("div_" + i).style.display = "block";
return
}
}
//ALERT WILL BE CALLED WHEN THE COUNT OF FILE UPLOADER IS 5 AND ALL THE DIV DISPLAY PROPERTY IS
// BLOCK
alert("Limit is over");
}
}

Thursday, December 11, 2008

Ruby: How "send" method works?

Invokes the method identified by symbol, passing it any arguments specified. You can use __send__ if the name send clashes with an existing method in obj.
For example.
Suppose that in you have a three different parameter
1.addnum
2.subnum
3.multinum

depends on the parameter you want to call the method to execute add/subtract/multiply. So what you can do

class Number

def mainfunction(methodname, val1, val2)
#THE BELOW MENTION SEND METHOD WILL CALL ANY ONE METHOD #DEPENDS ON THE FIRST PARAMETER
self.send(methodname, val1, val2)
end

def addnum(val1, val2)
val1 + val2
end

def subnum(val1, val2)
val1 + val2
end

def multinum(val1, val2)
val1 + val2
end
end

num = Number.new
num.mainfunction(“addnum”, 10 , 20) #output => 30

So this is just a very basic example to understand hoe send method work.