Argument Out of Range Rails 4 and Bootstrap3-Datetimepicker-Rails

Argument out of range Rails 4 and bootstrap3-datetimepicker-rails

@Uri Agassi is right: it's the values that you're passing to create the WorkOrder that are the most relevant here.

Try putzing around with the custom date formatting options provided to you by bootstrap-datepicker. If you can get a format that looks good and is easy to parse on the backend, then you'll want to intercept that parameter before it goes to the model for validation.

Regardless of what you'll go with for date formatting on the client-side, you'll still want to parse it into a useable Date or DateTime object server-side in your controller. Check out DateTime#strptime. You can also call #strptime on Date.

Untested, but the idea is there:

def create
order_params = work_order_params
order_params[:scheduled_date] = Date.strptime(order_params[:scheduled_date],
'%m/%d/%Y %I:%M %p')
@work_order = WorkOrder.new(order_params)
end

How to use bootstrap3-datetimepicker-rails gem on rails?

try this:

<script type="text/javascript">
$('.datetimepicker').datetimepicker({});
</script>

Redirect to another page on date select using datetimepicker in Rails?

Bootstrap3-datetimepicker offers you to plug to some events.

The event you're interested in is dp.change (see the previous link):

$('#datetimepicker1').on('dp.change', function (e) {
// use e.date for the new date, e.oldDate for the old date
// both are moment objects (from momentjs)
});

momentjs docs are here

jQuery Datepicker with Rails 4

Ok, I was able to get this working. There were two issues.
The first is that the date wasn't always getting saved into the database table because of a difference of format. So, I used a hidden field and used the altFormat and altField options of the datapicker. The second issue was formatting a current date when editing a record. This was achieved using strftime as DevDue suggested. So, for those that are interested here is what I have in my view. Not saying this is the best way since I really am still learning RoR, but it works :)

So, my partial view with the date:

<div class="span2 input">
<% dob_display = :dob %>
<%= f.text_field dob_display, placeholder: "mm/dd/yyyy", required: true, id: "cust_dob", value: @customer.dob.try(:strftime, "%m/%d/%Y") %>
<%= f.hidden_field :dob, id: "cust_dobalt" %>
</div>

It took me a while to realise that unless the hidden_field comes after the visble field it just doesn't work!

My application.js file for this datepicker looks as follows. Notice the altField and altFormat:

$(document).on('page:change', function () {
$('#cust_dob').datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "mm/dd/yy",
yearRange: "1900:+0",
altField: "#cust_dobalt",
altFormat: "yy-mm-dd",
duration: "slow"
});
});

Ruby on Rails, Datepicker search between two dates

You need to convert the string-formatted dates entered in the form (either via the DatePicker or manually-entered) to Date or DateTime values. When you query the database, use this DateTime#strptime to convert:

MyTable.where(date_field_to_search: DateTime.strptime(date_value_from_ui, "%F %T")

You can check out the formats available as the second argument to strptime, using the instructions at DateTime:strftime. Note that strftime and strptime are used to convert back and forth between DateTime and formatted String values.

In your Sailing#search(params[:q]) example, you can either implement the solution in Sailing#search (if you have access to change that code) or prior to calling Sailing.search.

Here is how you might implement this in Sailing#search:

class Sailing
def search(args)
datetime = args[:date_field_to_search]
if datetime.kind_of?(String)
datetime = DateTime.strptime(datetime, "%F %T")
end
# other search functionality
end
end

or, prior to calling Sailing#search:

datetime = params[:q][:date_field_to_search]
if datetime.kind_of?(String)
datetime = DateTime.strptime(datetime, "%F %T")
end
Sailing.search(params[:q].merge({ date_field_to_search: datetime })

Ruby on rails date picker not adding to database. What can i change?

You are using strong parameters to whitelist which require and permits only
those elements listed on your order_params permit method

Currently you will be getting params as params[ddate3] while you are permitting the dddate3 via orders i.e. params[order[ddate3]]

Now you need to set your dates as

var date_input=$('input[name="order[ddate3]"]')

and likewise.

Try this. Hope this Helps



Related Topics



Leave a reply



Submit