Rails 3.2 - best_in_place - Date value format issue while editing

432 views Asked by At

In my Rails 3.2 web application, I have used best_in_place gem to create a basic resume template. I have an issue with the the type :date. By default it is showing date in the yyyy-mm-dd format, but I need mm/dd/yyyy format both when displaying and editing. For changing the format in display, I have used a helper display_as: in best_in_place and I could see that the format has been changed.

The problem is that when I click on the value to edit it, it will again go back to the old format (yyyy-mm-dd). Here is the code for it.

View:

<div id="resume_template">
    <div id="user_personal_info" class="sub_template" align="center">
        <div class="title_section"><b>PERSONAL PROFILE</b></div>
        <hr>
        <table width="100%">
            <tr>
                <td valign="middle"align="right" width="35%">NAME</td>
                <td valign="middle"width="10%" class="colon_td">:</td>
                <td valign="middle"width="55%"><%= get_user_content('name') %></td>
            </tr>
            <tr>
                <td valign="middle"align="right" width="35%">EMAIL</td>
                <td valign="middle"width="10%" class="colon_td">:</td>
                <td valign="middle"width="55%"><%= get_user_content('email') %></td>
            </tr>
            <tr>
                <td valign="middle"align="right" width="35%">DATE OF BIRTH</td>
                <td valign="middle"width="10%" class="colon_td">:</td>
                <td valign="middle"width="55%"><%= best_in_place @resume, :dob, url: resume_path(@resume), type: :date, display_as: :format_dob %></td>
            </tr>
            <tr>
                <td valign="middle"align="right" width="35%">GENDER</td>
                <td valign="middle"width="10%" class="colon_td">:</td>
                <td valign="middle"width="55%"><%= best_in_place @resume, :gender, type: :select, collection: [[0, 'Male'], [1, "Female"]], value: @resume.gender %></td>
            </tr>
            <tr>
                <td valign="middle"align="right" width="35%">MARITAL STATUS</td>
                <td valign="middle"width="10%" class="colon_td">:</td>
                <td valign="middle"width="55%"><%= best_in_place @resume, :marital_status, type: :select, collection: [[0, 'Single'], [1, "Married"]] %></td>
            </tr>
            <tr>
                <td valign="middle"align="right" width="35%">LINGUISTIC ABILITY</td>
                <td valign="middle"width="10%" class="colon_td">:</td>
                <td valign="middle"width="55%"><%= best_in_place @resume, :linguistic_ability %></td>
            </tr>
            <tr>
                <td valign="middle"align="right" width="35%">NATIONALITY</td>
                <td valign="middle"width="10%" class="colon_td">:</td>
                <td valign="middle"width="55%"><%= best_in_place @resume, :nationality %></td>
            </tr>
        </table>
    </div>

    <div id="user_education_info" class="sub_template" >
        <div class="title_section"><b>EDUCATIONAL BACK GROUND/QUALIFICATION</b></div>
        <hr>
        <%= best_in_place @resume, :educational_info, type: :textarea %>
    </div>

    <div id="user_prof_exp_info" class="sub_template" >
        <div class="title_section"><b>WORK EXPERIENCE</b></div>
        <hr>
        <%= best_in_place @resume, :work_experience_info, type: :textarea %>
    </div>

    <div id="user_certification_info" class="sub_template" >
        <div class="title_section"><b>CERTIFICATION</b></div>
        <hr>
        <%= best_in_place @resume, :certification_info, type: :textarea %>
    </div>
</div>

<script type="text/javascript">
    $( document ).ready( function (){
        // $.extend($.fn.datepicker.defaults, { format: 'mm/dd/yy' });
        /* $.datepicker.setDefaults({
            dateFormat: 'mm/dd/yy'
        });*/
        
        jQuery(".best_in_place").best_in_place();
    });
</script>

Model:

class Resume < ActiveRecord::Base
  belongs_to :user
  attr_accessible :certification_info, :dob, :educational_info, :gender, :linguistic_ability, :marital_status, :nationality, :work_experience_info

  def format_dob
    self.dob.strftime("%m/%d/%Y")
  end
end

Attached two screenshots for reference. If it can't be seen, please download and zoom the picture:

enter image description here

3

There are 3 answers

0
Rajesh Omanakuttan On BEST ANSWER

I have found a solution, but I'm not sure how proper it is according to the Rails conventions.

Solution:

Just override the dob field value in the Model itself. Here it is Resume model.

class Resume < ActiveRecord::Base
  belongs_to :user
  attr_accessible :certification_info, :dob, :educational_info, :gender, :linguistic_ability, :marital_status, :nationality, :work_experience_info

  def dob
    if self.read_attribute(:dob)
        self.read_attribute(:dob).to_date.strftime("%m/%d/%Y")
    else
        self.read_attribute(:dob)
    end
  end
end

With this solution, we don't even require to use display_as helper with best_in_place. It will display the value in mm/dd/yyyy format both in the editable / non-edited modes.

0
VicML On

If you like to print date in default format, you need to configure initialized. Create a

initializers/date_format.rb

with this line

Date::DATE_FORMATS[:default] = "%m/%d/%Y"
0
Ricardosluiz On

try:
$.datepicker.setDefaults({ dateFormat: 'dd-mm-yy' });