Wednesday, June 17, 2009

Ruby on Rails functions to call function remotly(Ajax) or directly

#Remote Function Call
remote_function( :url => {:action => 'close_div'}, :with => "'div_id="+@div_id+"'") %>

# button_to_function
button_to_function "Greeting", "alert('Hello world!')"
button_to_function "Close", remote_function(:url => {:action => 'close_div'}, :with => "'div_id="+@div_id+"'")

# link_to_function
link_to_function "Greeting", "alert('Hello world!')"

# link_to_remote
link_to_remote "Delete this post", :update => "posts", :url => { :action => "destroy", :id => post.id }

Saturday, May 30, 2009

Pagination in Ruby on Rails

* Add following function in application.rb helper file


def pagination_links_remote(paginator)
page_options = {:window_size => 1}
pagination_links_each(paginator, page_options) do |n|
options = {
:url => {:action => 'list', :params => params.merge({:page => n})},
:update => 'table',
:before => "Element.show('spinner')",
:success => "Element.hide('spinner')"
}
html_options = {:href => url_for(:action => 'list', :params => params.merge({:page => n}))}
link_to_remote(n.to_s, options, html_options)
end
end


* Add code to fetch paginated results in controller file
e.g. @declined_patient_application_pages, @declined_patient_application = paginate :patient_applications, :per_page => 1, :conditions => { :application_status => 'Declined', :surgeon_id => session[:surgeon_login]}

* Call pagination in rhtml file
<%= pagination_links_remote(@declined_patient_application_pages)%>