List of Time methods

There are low of methods available on Time.now. Here it is exclusively for you :-

  • To find a current time and date – t=Time.now
  • To find the beginning of the day – t.at_beginning_of_day (or) t.at_mid_night
  • To find the beginning of the month  – t.at_beginning_of_month
  • To find the beginning of the quarter – t.at_beginning_of_quarter
  • To find the beginning of the week – t.at_beginning_of_week (or) t.monday
  • To find the beginning of the year – t.at_beginning_of_year
  • To find end of the month – t.at_end_of_month
  • To find end of the year – t.at_end_of_year
  • To find end of the quarter – t.at_end_of_quarter
  • To find end of the week – t.at_end_of_week
  • t.since(seconds) or t.in(seconds) will give you the time, which was after no of ‘seconds’ you give from the current time.
  • t.months_ago(months) will give you the date, exactly before the no of ‘months’ you give.
  • t.since(months) (or) t.next_month will give you the date, exactly after the no of ‘months’ you give.
  • t.next_week will get you the date of next week, same day
  • t.next_year will get you the date of next year, same day, same month.
  • t.past? will tell whether the date and time (t) is available on past.
  • t.future? will tell whether the date and time (t) is available on future.
  • t.today? will tell whether the date (t) is today
  • t.tomorrow will tell the time of tomorrow, from the object (t)
  • t.yesterday will tell the time of yesterday, from the object (t)
  • t.years_ago(years) will give the date of past, from the no of ‘years’ you give.
  • t.years_since(years) will give the date of future, from the current day to the no of ‘years’ you give.

How to use AJAX and JQuery on same page

If you are about to use AJAX and JQuery coding on the same page, it won’t work.

Don’t worry. Here is the solution,

Add this line on your layout or view file, wherever necessary

<head>

<script type=”text/javascript”>JQuery.noConflict();</script>

</head>

and then,

  • When calling jquery use ‘jQuery’ instead of ‘$’
  • When calling lightbox(ajax) use ‘document.getElementById’ instead of ‘$’

Example

<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Money Bee</title>
<link rel=”stylesheet” href=”stylesheets/login/style.css” type=”text/css” />

<link rel=”stylesheet” type=”text/css” href=”stylesheets/login/jquery-foxiblob-0.1.css” />

<%= javascript_include_tag ‘prototype.js’, ‘lightbox.js’%>
<%= stylesheet_link_tag ‘lightbox’%>
<script type=”text/javascript” src=”javascripts/login/jquery-1.3.2.min.js”></script>
<script type=”text/javascript” src=”javascripts/login/jquery-foxiblob-0.1.min.js”></script>
<script type=”text/javascript” src=”javascripts/login/jquery.corners.min.js”></script>
<script type=”text/javascript”>jQuery.noConflict();</script>
</head>

<body>
<script type=”text/javascript”>
jQuery(document).ready(function(){
jQuery(‘#menu_list’).foxiblob({opacity:0.6});
});
</script>

<script>jQuery(document).ready( function(){
jQuery(‘.rounded’).corners(“10px”);
});</script>

On the above, i have called many JQuery plugins and also used lightbox which gives AJAX request.

By inserting a line, <script type=”text/javascript”>jQuery.noConflict();</script> i made it work on the same page.

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()

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