firstruby

Master ruby with us

HUMANIZED_ATTRIBUTES in Rails 3

Posted by firstruby on January 21, 2011

In earlier version of Ruby on Rails 3, if we have some column like “dept_date”, in that case, error message is displayed as,

 

Dept date can’t be blank.

 

To change field/attribute name, we have to humanize this field. To humanized this field to display like “Departure Date” instead of “Dept date”  in earlier version of Rails 3 (i.e, in Rails 2 .x), then need to write like following,

HUMANIZED_ATTRIBUTES = {
:dept_date => “Departure Date”
}

def self.human_attribute_name(attr)
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end

In Rails 3, “def self.human_attribute_name(attr)” changes to “self.human_attribute_name(attr, options={})”, so above function will be as,

 

def self.human_attribute_name(attr, options={})
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end

 

Due to above code, dept_date field will be humanized to ‘Departure Date’.

Posted in Useful Links | 2 Comments »

Rails 3.0.x + Gem installation from git source + Passenger

Posted by firstruby on December 31, 2010

In Rails 3.0.3 application, I have developed login functionality using ‘Authlogic’ gem. Gem added in ‘Gemfile’ as,

gem 'authlogic', :git => 'git://github.com/odorcicd/authlogic.git', :branch => 'rails3'

Application was working fine with Mongrel and WEBrick server, but it was throwing an error with Passenger,

git://github.com/odorcicd/authlogic.git (at rails3) is not checked out. Please run `bundle install` (Bundler::GitError)

When we do ‘bundle install’, bundler installs gem (from GIT source) to different path other than regular one. Passenger does not understand this path, and that’s why above error was coming. So I have fixed it in following way,

I have installed ‘authlogic’ as a Plugin from branch “rails3” of given git path as,


cd vendor/plugins
git clone git://github.com/odorcicd/authlogic.git
cd authlogic
git checkout --track -b rails3 origin/rails3

After this, restarted application, and app works fine with Passenger, Mongrel, and WEBrick.

Hope this is helpful.

Posted in Useful Links | Tagged: , , , | Leave a Comment »

link_to_remote in Rails 3.x, AJAX link with link_to and :remote => true in rails 3.x

Posted by firstruby on August 8, 2010

In Rails < 3.x,

AJAX link_to_remote code is,

<%= link_to_remote "Click me", :url => update_user_url(user), :method => :put %>

In Rails >= 3.x, AJAX link_to is,

<%= link_to "Click me", user_url(user), :remote => true, :method => :put %>

Posted in Useful Links | Tagged: , , , | Leave a Comment »

link_to with HTML tags with linking text in Rails 3.x

Posted by firstruby on July 25, 2010

If there is HTML tag within tag like,

<a href=”#”><span>Click Me</span></a>

then to write the similar in rails >= 3.x is as follows,

In Rails >= 3.x,

<%= link_to raw("<span>Click Me</span>"), users_path %>

and in rails < 3.x,

<%= link_to "<span>Click Me</span>", users_path %>

Posted in Useful Links | Tagged: , | 1 Comment »

Restrict rake routes listing to particular Controller in Ruby on Rails

Posted by firstruby on May 9, 2010

We can restrict the routes listing to particular CONTROLLER by passing CONTROLLER variable as,

rake routes CONTROLLER=profiles

Above command will display routes of profiles controller.

Posted in Useful Links | Tagged: , , , | Leave a Comment »

Do we really need BackgrounDRb now ? Craken plugin makes job easier in Ruby on Rails

Posted by firstruby on January 1, 2010

There was some issue with BackgrounDRb like Memory leakage, so it was braeking the cron jobs. There are alternatives to BackgrounDRb as Craken. Craken is best.

Craken is a Ruby on Rails plugin for managing and installing rake-centric crontab.

Craken Installation as,

./script/plugin install git://github.com/latimes/craken.git

Craken Configuration with Ruby on Rails app,
You need to write the RAKE task which needs to run after some intervals.
Suppose I have a RAKE task db:send_emails

Create Crontab file at following path,

#{RAILS_ROOT}/config/craken/raketab.yml

Add configuration to raketab.yml as,

my_config:
hour: 0
minute: *
day: *
month: *
command: db:send_emails > /tmp/db_send_emails.log 2>&1

Above configuration will run the “db:send_emails” tasks after very 24 hours (midnight)

Finally create the CRON job with following command,

rake craken:install

Above command will create a cronjob.

For Production mode,
rake craken:install RAILS_ENV=production

You are done 🙂

More about it……..,
If we need to check the Cronjob, you can give the following command,

crontab -l

Following command displays the log once CRON job executes
tail -f /var/log/syslog

Ref URL – http://github.com/latimes/craken

Posted in Useful Links | 2 Comments »

resource_controller plugin with different associated model

Posted by firstruby on December 17, 2009

If we use resource_controller gem/plugin in Ruby on Rails application, then as per the default configuration, it takes the associated Model name with respective Controller name as,

in app/controllers/posts_controller.rb file,
class PostsController < ResourceController::Base
resource_controller
end

This above configuration will find for Post Model. But if you want to change it to associate it to different model as ‘Comment’ Model, then you can do it as,

class PostsController < ResourceController::Base
resource_controller


private
def model_name
'comment' # Model Name
end
end

Ref. URL – http://jamesgolick.com/resource_controller/rdoc/files/__/README_rdoc.html

Posted in Useful Links | Leave a Comment »

AJAX pagination in Ruby on Rails

Posted by firstruby on November 29, 2009

For users list, I wanted to have AJAX pagination.

For Ruby on Rails version 2.3.x,

1) Install will_paginate gem “config.gem “will_paginate”” or include will_paginate plugin in vendor/plugins.

2) Create helper file at /app/helpers/remote_link_renderer.rb with following content,
remote_link_renderer.rb
Here I’m attaching remote_link_renderer.rb.pdf file , copy contents from remote_link_renderer.rb.pdf and create file remote_link_renderer.rb

3) In controller action, write as following,

if request.xhr?
render :update do |page|
page.replace_html "users_list", :partial => "users", : object => @users
end
end

4) On “_users_list.html.erb” view page, write as,

= will_paginate @users, :renderer => 'RemoteLinkRenderer'

Posted in Useful Links | Tagged: , , , , | Leave a Comment »

Set default host for Mailer in RubyonRails

Posted by firstruby on October 16, 2009

We can set the default host for all of our methods in Mailer.

For this, we need to write one method in ApplicationController file with following code and call this method using before_filter,

ActionMailer::Base.default_url_options[:host] = request.host
or
ActionMailer::Base.default_url_options[:host] = request.host_with_port

Hope this will be helpful for someone.

Posted in Useful Links | Leave a Comment »

‘ordinalize’ in Ruby On Rails

Posted by firstruby on July 21, 2009

Print numbers as Cardinals in Ruby On Rails (like 1st, 2nd, 3rd, 4th and so on….)

Rails has inbuilt function – ordinalize

(1).ordinalize => "1st"
(13).ordinalize => "13th"
(22).ordinalize => "22nd"
(100).ordinalize => "100th"

Also we can write the helper as following,

def number_to_ordinal(num)
num = num.to_i
if (10...20)===num
"#{num}th"
else
g = %w{ th st nd rd th th th th th th }
a = num.to_s
c=a[-1..-1].to_i
a + g[c]
end
end

number_to_ordinal(23) => "23rd"
number_to_ordinal(1) => "1st"

Posted in firstruby, Useful Links | Leave a Comment »