Saturday, December 12, 2009

Simple Math-Logic-IQ-Captcha in Ruby On Rails

This is Engine based MVC patterned rails gem for Ruby On Rails Application

Github url: https://github.com/barek2k2/iq_captcha/

Sunday, December 6, 2009

Caching with rails

http://guides.rubyonrails.org/caching_with_rails.html

Friday, December 4, 2009

AJAX with Rails, Rails RJS

AJAX in Rails application with RJS
RJS - Ruby JavaScript - Generates and executes javascript code from your controller in AJAX request to process your Ruby code as well as updates your page's html DOM finally. To see details of how it works lets start

1. Suppose you have an ajax request call with an anchor tag in your view file like:
<%= link_to_remote('Click Me to show RJS',{:url => {:controller => 'experiment', :action => 'rjs'}, :loading => "", :complete => ''},{}) %>
this is content


And you want to update div of id 'id1' then
2. In Controller :
def rjs
    #you may have ROR stuff here
    render :update do |page|
        page.replace_html 'id1', 'This is content after AJAX call' # div with id1 will be replaced with the string
    end
end

Now click on the anchor then div of id ‘id1’ will be replaced by controller’s action’s method rjs’s response
Thats all of RJS basic with only 2 steps

Now if you want to replace div of id1 with another file’s content then you can do it like:
page.replace_html 'id1', :partial => "experiment/parts/part1", :locals => {:p1 => 1, :p2 => 2}
# part1 is your partial file (located in views/experiment/parts/_part1.html.erb)
# And you may pass parameters with locals p1, p2, …. pn
# you can catch this params in _part1.html.erb file like : <%= p1 %>
# You can also update more than 1 div in your view file with the same ajax request like:
page.replace_html 'id1', :partial => "experiment/parts/part1"
page.replace_html 'id2', ‘Another String’

page.remove ‘remove_div_id’
page << “js_mehtod()” # where js_mehtod() is the javascript function which resides in your view.

Now AJAX call with form submit event
this is content

<% form_remote_tag :url => {:controller => 'experiment', :action => 'rjs'}, :loading => '', :complete => '', :html => {:method => :post} do %>

<%= text_field :name %>

<%= submit_tag(Submit) %>

<% end %>





To know more visit: http://www.developer.com/lang/print.php/10924_3668331_2

Wednesday, December 2, 2009

How to execute SQL Query in rails Application



connection = ActiveRecord::Base.connection();

Example :
1. connection.execute(any_sql_query)

2.
results = connection.execute("select * from users")
results.each do |row|
puts row[0]
end

Extending Rails ActiveRecord::Base Class

Example:
1. Create a file(suppose init.rb) into your project's config/initializers folder

2. Paste the following code :
ActiveRecord::Base::class_eval do
def self.your_method
return 'Now I am in ActiveRecord'
end
end

3. Now you can call your_method from any Model of your application, i,e you are adding your_mothod(it will static method) to all your application's models

4. You can call it like :
User.your_method()
Post.your_method() etc...

Tuesday, December 1, 2009

Ruby open class in Rails application, extending Fixnum,Array,Date,String Class

1. Make a library file(suppose "your_extension.rb") into your project's lib folder

2. require File.join(RAILS_ROOT,'lib/your_extension.rb')
- Add the above line at the End of environment.rb file

3.Suppose you want to extend Ruby Fixnum Class with your mehtods(named prime?) then PASTE the following code into "your_extension.rb"

class Fixnum
  def prime?
    n = self
    counter = 0
    1.step(n,1) {|i|
      if(n%i == 0)
        counter += 1
      end
      }
    if(counter == 2)
      return true
    else
      return false
    end
  end
end

4. Restart your Rails application and you are done!
5. Now you can call prime? method from anywhere of your Rails application like
2.prime? -- returns true
5.prime? -- returns true
10.prime? -- returns false