firstruby

Master ruby with us

Posts Tagged ‘rails’

Mysql::Error: query: not connected while rake db:migrate

Posted by firstruby on August 25, 2011

With Ruby 1.9.2 and Rails 3.0.10,
If you are facing following problem while doing “rake db:migrate”,

Mysql::Error: query: not connected: SHOW TABLES

then you may need to download ‘libmySQL’ from http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll and paste that in C:\Ruby192\bin folder.

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

OpenSSL::Cipher::CipherError (Illegal key size) in jRuby on Rails application

Posted by firstruby on July 17, 2011

If you got following error,

OpenSSL::Cipher::CipherError (Illegal key size)

it means you need to install ‘Java Jurisdiction policy files’. These files can be downloaded from http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html and copy ‘local_policy.jar’, ‘US_export_policy.jar’ and replaced at ‘C:\Program Files\Java\jdk1.6.0\jre\lib\security’.

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

HTTP authentication for Ruby on Rails application

Posted by firstruby on June 23, 2011

You may want to add HTTP authentication to your Ruby on Rails site, add following to your application controller file,


before_filter :authenticate

protected

def authenticate
authenticate_or_request_with_http_basic do |username, password|
username == "test" && password == "test"
end
end

After adding these lines, application will ask for username and password.

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

Language fallback mechanism in Ruby on Rails

Posted by firstruby on May 29, 2011

Language fallback mechanism in Ruby on Rails is very useful. It is useful in 2 ways,

1) File missing : If particular locale file say German locale ‘de’ is set from browser, then Rails application will look for de.yml file in /config/locales folder, and if de.yml is missing, then by default language fallback mechanism will load en.yml file as default.

2) Entry missing: If German locale ‘de’ is set from browser, then Rails application will look for de.yml file in /config/locales folder. In case, particular entry is missing in de.yml file, then conversion for the same language will be loaded from en.yml file.

To achieve this, just need to install latest I18n gem and need to create file /config/initializers/i18n.rb file with following,

I18n.backend.class.send(:include, I18n::Backend::Fallbacks)

Reference URL : http://stackoverflow.com/questions/2324398/fall-back-to-default-language-if-translation-missing

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

Regular Expression to skip alphabets, accepts numbers and few characters

Posted by firstruby on February 20, 2011

Following is the Regular expression which accepts numbers, plus (+), miuns (-) symols and skips alphabets,

RegEx:   /\A[+\-0-9]+\z/

This RegEx can be useful for Phone number validation.

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

Call Helper method from ActionMailer views in Ruby on Rails application

Posted by firstruby on February 2, 2011

If you want to call helper method from ActionMailer view in Ruby on Rails (RoR) application, you need to include helper (module) in Mailer file as,

If Helper module name is “UserHelper”, then need to write following in Mailer file,

add_template_helper(UserHelper)

After this, you will be able to access all the methods written in UserHelper in ActionMailer views.

Hope this is helpful.

Posted in Useful Links | Tagged: , , , | Leave a 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 »

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 »

ri and rdoc in Ruby On Rails

Posted by firstruby on November 16, 2008

To install ri and rdoc of any gem,

gem install gemname

This command installs ri and rdoc with installation of gem.

To skip the installation of ri and rdoc of gem,

gem install gemname –no-ri –no-rdoc

This command will skip the installation of ri and rdoc when installing gem.

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

remote_function or link_to_remote with multiple parameters in Ruby on Rails

Posted by firstruby on November 3, 2008

We can pass the multiple parameters with remote_function or link_to_remote…

<%= link_to_remote "View Data",
:url => :action => "list",
:with => " 'name=' +$('div-id-of-name-text-box').value + '&city=' +$('div-id-of-city-text-box').value + '&country=' +$('div-id-of-country-text-box').value " %>

or

<%= remote_function(:url => :action => "list",
:with => " 'name=' +$('div-id-of-name-text-box').value + '&city=' +$('div-id-of-city-text-box').value + '&country=' +$('div-id-of-country-text-box').value " ) %>

Posted in Useful Links | Tagged: , , | 3 Comments »