How to setup mongrel cluster setup on fedora

First install the following gems:
#su
#gem install mongrel
#gem install mongrel_cluster
#cd project_name
#mongrel_rails cluster::configure -e production -p 3000 -N 3 -c /home/siwan/project_name -a 127.0.0.1 —-prefix /project_name
# mongrel_rails cluster::start

You are able to start your applicaton at http://127.0.0.1:3000, http://127.0.0.1:3001 and http://127.0.0.1:3002

for all the cluster
# mongrel_rails cluster::stop

Advanced prepairation for production realeaze
$ mkdir /etc/mongrel_cluster

#vim /etc/mongrel_cluster/project_name.yml
copy and paste following text;
user: project_name
cwd: //home/siwan/project_name
log_file: /home/siwan/project_name/mongrel.log
port: “3000”
environment: production
group: dev
address: localhost
pid_file: /home/siwan/project_name/tmp/pids/mongrel.pid
servers: 3

or you can run the following command

or copy and paste the content from config/mongrel_cluster.yml file to /etc/mongrel_cluster/project_name.yml

# ln -s /home/siwan/project_name/config/mongrel_cluster.yml /etc/mongrel_cluster/project_name.yml

Then open your httpd.conf file for apache configration:

<Proxy balancer://project_name>
BalancerMember http://127.0.0.1:3000
BalancerMember http://127.0.0.1:3001
BalancerMember http://127.0.0.1:3002
</Proxy>

<VirtualHost *:80>
ProxyPreserveHost On
# Avoid open you server to proxying
ProxyRequests Off
# Options +FollowSymLinks
RewriteEngine On

RewriteRule ^/(images|stylesheet|javascript|html)/?(.*) /home/siwan/project_name/public/$0 [L]
ServerAdmin siwan@yahoo.co.in
DocumentRoot /home/project_name/
ServerName example.com
RewriteRule  ^/(.*)$  balancer://project_name%{REQUEST_URI} [P,QSA,L]
</VirtualHost>

Restart the apache server
#/etc/init.d/httpd restart

Command for restart the mongrel servers from any where
# mongrel_rails cluster::restart -C /etc/mongrel_cluster/project_name.yml

Use PHP in Rails project

Many times you need to use PHP scripts in Rails project. You will got so much open source PHP scripts for various use.

Example is So many Rails site is using WordPress for blogging system.

Main reason behind using PHP files or script is SEO. PHP is a very SEO friendly.

Customer does not want to spend money or time already existing scripts or program.

If you want to use PHP files or project under Rails project. Just create any folder in Public directory and put your php files in to that folder.

lets say you created the “fourm” dir in public directory.

You need to add following lines in your apache rule file(httpd.conf)

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_http_module modules/mod_proxy_http.so

Under <VirtualHost  *:80> tag add following lines:

ProxyRequests Off

RewriteEngine On

RewriteCond  %{HTTP_HOST}    ^example.com [NC]
RewriteCond %{REQUEST_URI}  !^/forum(.*)$ [NC]

RewriteCond  %{HTTP_HOST}    ^example.com [NC]

RewriteRule ^/forum/?(.*)$ /document_root/forum/$1 [QSA,NC,L]

Than you are able to use PHP code or scripts in Rails project

How to use juggernaut in Rails project

Juggernaut is the Rails plugin for sending and receiving data in different thread. It gives you real time connection to server and you can implement different ideas work or fulfil your requirement.

Juggernaut uses the eventmachine as a server.

So install the supported gem first.

#gem install json

#gem install eventmachine

#gem install juggernaut

Juggernaut is aims to revolutionize your Rails app by letting the server initiate a connection and push data to the client.

Juggernaut is used for speciallycreating Chat application.

If you want to full details about Juggernaut then go to http://juggernaut.rubyforge.org/

First install the juggernaut to your application using this command.
ruby script/plugin install git://github.com/maccman/juggernaut_plugin.git

install the gem : gem install juggernaut

Configure the gem: juggernaut –g juggernaut.yml

Start the Juggernaut server: juggernaut -c juggernaut.yml

(This is not rails server or mongrel server)

In your controller you can use this method to send data to Juggernaut server

Juggernaut.send_to_all(“alert(‘hi from juggernaut’)”)

If you want customize the Juggernaut methods or add new methods then open file:

/vender/plugin/juggernaut_plugin/lib/juggernaut.rb

Add your methods there and use in your projects.

link_to with image_tag in Rails

This is very basic form tag in rails. but some time you get confused about using this tag.

How to displays a image inside link_to instead of text?

Here is answer:
<%= link_to image_tag(“/images/submit.jpg”, :border=>0), :action => ‘create’ %>

Section Option:

<%= link_to image_tag(“/images/submit.jpg”, :border=>0,:class=>’sub20′), :url =>{:controller=> ‘user’,:action=>’create’}  %>

Create XML document with Ruby on rails

XML is most popular data transfer layer. In every language we need XML parsing. In Ruby there is in build XML strong parsing support.

Create XML document with Ruby on rails

In this post I am going to cover the How to create XML file with simple Ruby code.

TIP: If you are new in Rails than follow step one or go to step two.

1. You need to install Ruby on your computer

http://www.ruby-lang.org/en/

2. Open command prompt(for windows) or console(linux)

3. Create file named CreateXML.rb and copy and paste following code

require “rexml/document”
include REXML

string = <<EOF
<xml>
<element attribute=”attr”>first XML document with ruby </element>
</xml>
EOF
doc = Document.new string

print doc

Output will be like this:

Create XML document with Ruby on rails
Create XML document with Ruby on rails

text_area tag in Rails – Issues

When you start New Rails project.  Always we used scaffolding for creating simple base for out application.

text_area tag in Rails – Issues

I used scaffolding for some methods. I got some very weird issue about tex_area tag in rails.

When i check the forms textagea fields are coming with 20 rows and 40 cols default. When i tried to change that is not changing even though CSS also.

<textarea id=”dummy_text” rows=”20″ name=”dummy_text” cols=”40″/>

After digging into rails code i got know that Rails ->Actionpack-> form_helper.rb file has default setting of textarea field.

def text_area(object_name, method, options = {})
InstanceTag.new(object_name, method, self, options.delete(:object)).to_text_area_tag(options)
end

We can change this also. but this is not good idea

so good idea is select our project and use find replace:

text_area to text_area_tag

This is the simplest solution.


Rich Text Editor in Rails Application

Many times you need normal CMS for your application. You want to need some pages data need to handle through CMS.

It is really very easy to Install any RTE in the Rails. There are many open source RTE available.

I used the Cross-Browser Rich Text Editor for my project.
I downloaded files from there and uploaded to my public folder. I added required CSS and JS file to my layout.

You need to add following lines to your application.html.erb file.(you will find this file in view/layout/ folder)

1. You can add that files on conditional base also. Means for that particular page.
2. Add following lines to your form
:html => { :name => ‘BlogRTE’, :onsubmit => “submitForm();”}

In to add following lines to your form;

<script language=”JavaScript” type=”text/javascript”>
<!–
function submitForm() {
updateRTEs();
return false;
}
initRTE(“/cbrte/images/”, “/cbrte/”, “”, true);
//–>
</script>
<noscript><p><b>Javascript must be enabled to use this form.</b></p></noscript>
<script language=”JavaScript” type=”text/javascript”>
<!–
//build new richTextEditor
var rte1 = new richTextEditor(‘text_content’);
rte1.html = ‘Write your thoughts here.’;
rte1.toggleSrc = true;
rte1.width = 500;
rte1.build();
//–>
</script>

You will get the text_content params in Rails.

For Edit page functionality i got error in form. So you need to this default code for all languages.
rte1.html =””;

When i used this i got an error.

But i solved this issue after some R&D and modification in code.

Use following code for Rails(Edit functionality)
rte1.html =”<%=text_content.gsub(/”/, “‘”).gsub(/\n/, ”).gsub(/\r/, ”) %>”;

this will solve your problem.

Full code: (IF YOUR CODE NOT WORKS THAN USE MY FULL CODE)

<script language=”JavaScript” type=”text/javascript”>
<!–
function submitForm() {
updateRTEs();
return false;
}
initRTE(“/cbrte/images/”, “/cbrte/”, “”, true);
//–>
</script>
<noscript><p><b>Javascript must be enabled to use this form.</b></p></noscript>
<script language=”JavaScript” type=”text/javascript”>
<!–
//build new richTextEditor
var rte1 = new richTextEditor(‘text_content’);
rte1.html = ‘Write your thoughts here.’;
rte1.toggleSrc = true;
rte1.width = 500;
rte1.build();
//–>
</script>

Rails if else statement

Here i am going to give some example of conditional statements in Rails.
Normal IF ELSE
<% if user %>
<%= user.name %>
<% else %>
Anonymous
<% end %>

Normal code for ELSIF
if var == 10
print “Variable is 10″
elsif var == “20″
print “Variable is 20″
else
print “Variable is something else”
end

Technic:
How to Put this in one line:
<%= user.name if user %> This is the simplest way.

How to use IF and ELSE statement in one line:
<%= user ? user.name : “Anonymous” %>

fetch svn project without svn files and without deleting

I worked with any technologies and languages. Many times we need some folder or codebase for your new project.
So we need to copy old project code or folder in our new project. In rails we need many plugins for our project. Some times few plugins are not getting installed in our project.

fetch svn project without svn files and without deleting

Few days back i tried to install acts_as_solr plugin in my project. I got an error. So i coppied acts_as_solr plugin from my old project and pasted in new project.

When i tried to SVN commit the files in the new project. That files started commiting to Old project.
So i need to remove all .svn folders form all the subfolders. Than only i can add all the folder to new project.

I google around for solution. I found many ways:

  • Remove the all .svn folder from all subfolder.
  • Through linux command find all .svn file and delete. (but i think this harmful)
  • SVN export – command

SVN export is the very simple and reliable way to fetch project out of svn.
In windows, using svn export is very easy.

Paperclip with Rails for image manipulation

I used the spree e-Commerce CMS for checking or R&D of Spree code. Spree is the really nice and basic tool for e-Commerce CMS.

When i was going through Spree i got to know about paperclip plugin which is used for image manipulation. Earlier i used “attachment_fu” for image manipulation and file uploading in rails projects.

How to use “Paperclip

First install perperclip plugin to your project.

Windows and Linux user can use my code. (I used this in WindowsXP and Fedora 9)

#ruby script /plugin install https://svn.thoughtbot.com/plugins/paperclip/trunk/

through this command perperclip get installed in your project folder.

Many time you need photo upload functionality for customer

Here i used Customer contoller and Customer model for this lession

First run following command

#ruby script/generate paperclip ModelName FieldName

In my case command is:

#ruby script/generate paperclip Customer customer_pic

One migration file will be created through this command. Run that migration.

In Customer model file paste this code:

class Customer < ActiveRecord::Base
# Paperclip
has_attached_file :customer_pic,
:styles => {
:thumb=> "100x100#",
:small => "150x150>" }
end

Using this command you will save three pic(photo) in your system folder.

Default upload url of peperclip is (RAILS_ROOT/public/system/…)

You can use following lines in your model file. (copy & paste this code under styles code)

:url => “/uploads/:class/:attachment/:id/:style_:basename.:extension”,
:path => “:rails_root/public/uploads/:class/:attachment/:id/:style_:basename.:extension”

I added uploads folder front on :class. That is optional you can remove also.

If you are already having forms for customer and if you want to add that to form. Just use this code in your form tag.

:html => { :multipart => true

and in from <%= f.file_field :customer_pic%>

That sit. you need to add or use following code in View

<%= image_tag @user.customer_pic.url %>

<%= image_tag @user.customer_pic.url(:thumb) %>

Paperclip Validations

Here i giving some validation for Paperclip. You need to just copy & paste in to your model where you want to use image upload.

validates_attachment_content_type :avatar, :content_type => 'image/jpeg'

validates_attachment_presence :avatar

I found this plugin is very usefull for Me. It really saves lots of time.