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:
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.With this solution, we don't even require to use
display_as
helper withbest_in_place
. It will display the value inmm/dd/yyyy
format both in the editable / non-edited modes.