Callback methods

after_create()

This will be called after saving the object (for eg.., @sample.save). At this time no record exists.

after_destroy()

This will be called after destroying the object (for eg.., @sample.destroy)

after_save()

This will be called after saving the object (for eg.., @sample.save),  regardless of create (or) update

after_update()

This will be called after saving the object (for eg.., @sample. save), on the existing objects that have the record.

after_validation()

This will be called after the validation has been done (regardless of create or update)

after_validation_on_create()

This will be called after the validation on new object that has not yet been saved.

after_validation_on_update()

This will be called after the validation on the existing objects that has the record.

On the same way we can use the following methods respectively, for calling the method before doing some operations.

  1. before_create()
  2. before_destroy()
  3. before_save()
  4. before_update()
  5. before_validation()
  6. before_validation_on_create()
  7. before_validation_on_update()
Advertisements

link_to_function and button_to_function

If you need to refresh a part of a page in rails, sometimes we
use link_to_remote  (for replacing a partial file ), which is
a remote link and when clicking on that link, it will call a
AJAX method on controller and there we will replace the
partial file. This is not the only solution for doing this,
we can even do these kind of things without going to the
controller methods (if no DB part is involved)

link_to_remote & button_to_remote

Just put this code on your view file

1. For just displaying an alert message

button_to_function "Greeting", "alert('Hello world!')"
(OR)
link_to_function "Greeting", "alert('Hello world!')"

2. if u have to call a JS function after confirm box

button_to_function "Delete", "if (confirm('Really?'))
do_delete()"
(OR)
link_to_function "Delete", "if (confirm('Really?'))
do_delete()"

3. For making visual effects, replacing partial,
showing or hiding an html id element etc..,
 button_to_function "Details" do |page|
 page[:details].visual_effect :toggle_slide
 page[:details].replace_html "partial_file_name"    
 page[:details].show
 page[:details].hide
 end

(OR)

 link_to_function "Details" do |page|
 page[:details].visual_effect :toggle_slide
 page[:details].replace_html "partial_file_name"    
 page[:details].show
 page[:details].hide
 end

Note : Here 'details' is the div (0r) any html id element.
"Details" is the link (or) button that is to be displayed
on your view file. 

4. We can also apply the class for the button. 

button_to_function "Details", :class => "details_button"
do |page|
   page[:details].visual_effect :toggle_slide
end