Input multiple date from a calendar (Ruby on Rails) - ruby-on-rails

I'm creating a leave management app, and I need to input multiple date into a field from calendar (jquery ui datepicker or something like that).
I've tried gem 'multi-dates-picker-rails', but it's not working.
Would you give a solution? Thank you.

You can use below jquery library file
http://dubrox.github.io/Multiple-Dates-Picker-for-jQuery-UI/
Javascript
https://raw.githubusercontent.com/dubrox/Multiple-Dates-Picker-for-jQuery-UI/master/jquery-ui.multidatespicker.js
CSS
https://raw.githubusercontent.com/dubrox/Multiple-Dates-Picker-for-jQuery-UI/master/jquery-ui.multidatespicker.css
include these file into assets
In html
<%= text_field_tag :route_dates, nil, class: "gui-input", placeholder: "Select date" %>
coffescript
date = new Date()
$('#route_dates').multiDatesPicker
addDates: [date, date]
dateFormat: 'dd-M-yy'

Related

Rails 6 : use a HTML5 date field for a Ransack search filter

I have a list of appointments, with a ransack search field to filter by date. As of now, users have to type the date, like "12/02/2022". Elsewhere in the app, I'm using a HTML5 date selector, and I've been asked to use the same type of field for the Ransack filter.
I wish I could do something like this :
<%= f.search_field :appointment_date_eq, as: :date %>
But it just don't work. I don't want to use Jquery UI, so how can I do this ?
<%= f.search_field :appointment_date_eq, type: :date %>

bootstrap get crazy date format when editing a date in rails

So, I have a rails application which there is a datepicker. When I put the date for the first time in the calendar it appears in this format: dd-mm-yyyy.
But rails save in the database in this format: yyyy-mm-dd.
When I go to edit this in the form, the datepicker get crazy, it appears a 1900 year, not the actual year.
I know this happens because the format date is different(the date which bootstrap expects and the date which is saved).
I tried change the datepicker format in the edit view(.html.erb) but it doesn't work. How can I solve this?
<%= f.text_field :my_date, "data-provide" => 'datepicker',
'data-date-format' => 'dd-mm-yyyy', class: 'datepicker form-control', :required => true %>

Rails display American date format when using Bootstrap datepicker

I'm trying to use the guidance here to convert my date format in my model. The reason I am doing this is I want my user to see the date format as 7/20/2014, but I need to save that date into the database as 2014-7-20. I tried to implement that with this code, but it is not changing the date format when creating or reading:
class Inquiry < ActiveRecord::Base
belongs_to :user
def date_requested
read_attribute(:date_requested).strftime("%m/%d/%Y")
end
def date_requested=(date_requested)
write_attribute(:date_requested, date_requested.strftime("%Y-%m-%d"))
end
end
Ok, I figured this out. Here's what I did to use American style dates with Bootstrap datepicker:
Add the gem american_date to your gemfile: gem 'american_date'. This gem parses a date as m/d/yy into the format required by the database.
Run bundle install in terminal
Set your form field to show a value rather than what is actually in the database:
<%= f.text_field :requested_date, :value => #inquiry.requested_date.strftime("%m/%d/%Y"), 'data-behaviour' => 'datepicker' %>
Edit your index or show file to display the American format: <%= #inquiry.requested_date.strftime("%m/%d/%Y") %>
This is working great for me.

Rails 3.1 JQuery Icons

I'm trying to attach a JQuery datepicker to a text field on a form, but I'm not sure how to use the icons that come with the JQuery download.
I have all of the stylesheets in my app/assets/stylesheets folder and there is a subfolder called images which I have also put in the stylesheets folder. I saw a different StackOverflow post saying that I shouldn't put those images into my app/assets/images folder.
Then in my .coffee file I have
$ -> $('.datepicker').datepicker({
buttonImageOnly : true,
buttonImage : "calendar.gif",
showOn : "button"
})
The problem is that I don't have a file called calendar.gif. I've seen a lot of examples for buttonImage and all of them reference files that I don't have. What I do have is several .png files that seem to have all of the icons for the theme in them. Is there something else that I am supposed to download or am I putting the big .png file in the wrong place?
I have tried so many different values for buttonImage that I wont bother to list them all. Here is a selection of stuff I've tried:
ui-icon-calendar
images/ui-icon-calculator.png
images/.ui-icon-calculator.png
images/ui-icon-calculator
Any help is greatly appreciated.
This is how I load my datepicker:
_form.html.haml
.row
.span16
= normal_div_if #purchase_order.errors[:date].empty? do
= f.label :date, "Date, class: "compulsory"
.input
- if #purchase_order.new_record?
= f.text_field :date, id: "datepicker", class: "small"
- else
= f.text_field :date, id: "datepicker", class: "small", value: l(#purchase_order.date)
global.js.coffee.erb (my custom javascript)
$ ->
# Initiate date picker
$("#datepicker").datepicker({dateFormat: "dd-mm-yy", changeMonth: true, changeYear: true, showOn: "button", buttonImage: "<%= asset_path('calendar.png') %>", buttonImageOnly: true});
I put my calendar image is at assets/images/calendar.png
You can get the calendar image at http://www.famfamfam.com/lab/icons/silk/ for free.
I think you're asking about using the datepicker field with the icon trigger. This variant is demonstrated on this page:
http://jqueryui.com/demos/datepicker/#icon-trigger
If this is the case, it appears that the icon image is not an asset that is included with jQuery UI. You will need to create or find a calendar icon.

Rails formatting in edit field

Can rails formatting helpers be used on an 'edit' screen? The format helpers (number_to_currency, number_to_percent) are great for index/show, but I don't see how to apply them during edit. I have a custom heler that formats the date:
def my_date_helper(datetime)
datetime.nil? ? "" : datetime.strf('%d-%b-%Y')
end
For example, if I have a starts_at attribute, that the user interacts with using a jQuery datepicker, the value placed in edit.html.erb in <%= f.text_field :starts_at %> by rails will be formatted like:
2011/12/19 00:00:00
I would like the user to be presented with a consistent format,so I want to apply the same format helper I use in show/index so the edit text field shows a format like:
19-Jan-2011
You could do this for your edit form:
<%= f.text_field :starts_at, :value => my_date_helper(#my_model.starts_at) %>
Also, jQuery's Date Picker includes a dateFormat parameter, so you could do something like this in your javascript:
jQuery("#my_model_starts_at").datepicker({
dateFormat: 'd/M/yy'
});
I just had a 'duh' moment. To doesn't seem there's any easy, rails-ey way. But it can be done really easy with unobtrusive JavaScript, like:
$('.datepicker').each(function(i){
var date = new Date($(this).val());
$(this).val($.datepicker.formatDate('dd-M-yy', date));
});
Four lines in application.js and you're done.
jQuery('#datetimepicker3').datetimepicker({
format:'d.m.Y H:i',
inline:true,
lang:'ru'
});
datetimepicker

Resources