Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Date and Time Helpers in Rails

    • 0
    • 2
    • 2
    • 1
    • 0
    • 0
    • 0
    • 0
    • 878
    Comment on it


    Date and Time Helpers in Rails

    Date and time selection are very common in forms for saving event times, schedules, birth date etc. Rails also provides date and time helpers for taking inputs of date, time or datetime. While dealing with date-time, as they are a combination of multiple element, i.e. date(combination of year, month and day), so we will get multiple elements in params hash for date-time. Similar to other helpers, these helpers can also be divided into two parts: barebone and model specific.

    barebones : -
    These type of helpers are select_date, select_time and select_datetime for date, time and datetime respectively. These helpers take date, time or datetime instances as first argument, you can also omit this argument and give today's date, current time etc. For example if a employee wants to apply for leave, the form will be created as:

    <%= select_date Date.today+1, prefix: :leave_date %>
    #Date.today+1 is used to give the tomorrow's date as selected
    


    It will give as output as:

    <select id="leave_date_year" name="leave_date[year]"> ... </select>
    <select id="leave_date_month" name="leave_date[month]"> ... </select>
    <select id="leave_date_day" name="leave_date[day]"> ... </select>
    


    This will give parameters in params[:leave_date], which will be a hash with keys year, month and day. To get actual object you can pass them to a constructor:

    Date.civil(params[:leave_date][:year].to_i, params[:leave_date][:month].to_i, params[:leave_date][:day].to_i)
    


    date time helpers with models : -
    Unlikely other helpers date-time helpers doesn't remove '_tag' for using with models. For models date_select, time_select and datetime_select helpers are used for date-time selection in forms. These helpers for dates and times submit parameters with some special names and when Active Record sees such names it knows that they must be combined with some other parameters and need to be given to a appropriate constructor. For example:

    <%= date_select :employee, :joining_date %>
    
    # = >
    <select id="employee_joining_date_1i" name="employee[joining_date(1i)]"> ... </select>
    <select id="employee_joining_date_2i" name="employee[joining_date(2i)]"> ... </select>
    <select id="employee_joining_date_3i" name="employee[joining_date(3i)]"> ... </select>
    


    This will send parameters as a hash to server in this form.

    {
        'employee' => 
            {   'joining_date(1i)' => '2015', 
                'joining_date(2i)' => '03', 
                'joining_date(3i)' => '25'
            }
    }
    


    When server gets this kind of parameters, it knows that, it needs to be saved in joining_date and in which order it needs to be passed to Date.civil constructor.

    Hope you liked this. For more like this, Click here.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: