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
Testing Rake Task with Rspec with Rails Environment
How Do Get a Random Datetime Rounded to Beginning of Hour in Rails
Why Do I Need Asterisk Before an Array
How Does One Downgrade Jekyll to Work with Github Pages
Installing Pl/Ruby for Postgresql 8.3
How to Programmatically Take Snapshot of Crawled Webpages (In Ruby)
User Selected CSS Stylesheet in Rails
Installing Rails with Gem, Error Loading Command: Install Undefined Method 'Invoke_With_Build_Args'
Sqlite3-Ruby Gem Can't Find SQLite3.H on Ubuntu
Determining Whether One Array Contains the Contents of Another Array in Ruby
When Trying to Generate a Model with Rails and Postgresql, the Command Hangs Without Error
How to Send a Parameter to a Before Filter
I Can't Remove Whitespaces from a String Parsed by Nokogiri
How to Pass Named Arguments to a Rake Task
How to Import Using Fastercsv a Row with a Name Like "CiaráN"