Rails simple-form datepicker how to add data-behaviour - ruby-on-rails

I have a Rails app using jquery-ui datePicker.
Inside of a simple_form I have two fields. The first one is just a test field. The first one has datePicker working. I'm trying to get "data-behaviour" into the simple_form f.input line. But, I've got the wrong syntax.
<input type="text" data-behaviour='datepicker' >
<%= f.input :exp_date, :data-behaviour => 'datepicker', :as => :string, :label => 'Date', :input_html => {:value => Date.today.to_s} %>
This is my jquery:
$(document).on "focus", "[data-behaviour~='datepicker']", (e) ->
$(this).datepicker
format: "yyyy-mm-dd"
weekStart: 1
autoclose: true
How can I get the data-behavior='datepicker' into the f.input line?
Thanks!

Try something like
#old hash syntax
:data => { :behavior => "datepicker" }
#new syntax
data: { behavior: "datepicker" }

Related

Rails 4 Bootstrap DatePicker class today not working

I have a Rails4 app using gem 'bootstrap-sass'. I'm using the bootstrap datepicker.
This is the Javascript:
$(".datepicker").datepicker
autoclose: true
format: "yyyy-mm-dd"
orientation: "left top"
This is the view code:
<%= f.input :shoot_date, :as => :string, :label => 'Shoot Date:', :input_html => {:class => 'datepicker'}, :wrapper_html => {:style => 'width: 250px'} %>
But, today's date is not highlighted. In the html, it should have:
<td class="day today">23</td>
But it is:
<td class="day">23</td>
The today is missing.
What am I doing wrong? Thanks
PS - I don't want to default to today's date - just have it highlighted on the calendar
Assuming you are using this bootstrap-datepicker, https://github.com/eternicode/bootstrap-datepicker
You should be doing something like this:
$('.my-datepicker-class').datepicker({ todayHighlight: true});
todayHighlight is the field you have to set to true
hope it helps!

What could be causing nested_form not to output the correct chosen input value?

So I'm using nested_form (v0.3.2 according to Gemfile.lock) with Rails 3.2.11. I have a Service model with a category field that initially can take on multiple values that can be input via a select dropdown.
The categories are something like: ["J Award", "Z Award", "Other" ]
When I go to save the form fields with a value of J Award (or Z Award) and rerender an editable form, the form redisplays with a value of "Other" for category. Yet if I step into the rails console and look at the category field of the saved service model, it shows "J Award".
What could be causing this? Since nested_form is no longer maintained, should I just give up and handle multiple models in a single form differently?
haml output of rerendered form
= semantic_nested_form_for #service, :url => "/update", :html => { :class => "service", :autocomplete => "off" } do |f|
%h1.page-title Service
.page-wrapper
= render :partial => "shared/error_messages", :locals => { :object => #service }
html output of rerendered form:
<li class="string input required stringish" id="service_category_input"><label class=" label" for="service_category">Category<abbr title="required">*</abbr></label><input id="service_category" maxlength="255" name="service[category]" type="text" value="J Award" />
</li>
= f.inputs do
%h3 Project Information
= f.input :billable, :as => :radio, :collection => { 'Billable' => true, 'Non-billable' => false }, :label => 'Category', :input_html => { :disabled => true }
= f.input :category
= f.input :assigned_consultant, :input_html => { :readonly => true }
= f.input :aims, :input_html => { :readonly => true }
In your html.erb file, I'd change the f.input to f.select:
<%= f.select :category, ["J Award", "Z Award", "Other"], {selected: f.object.category} %>
The selected attribute is self-explanatory. I've never worked with .haml files before, so I guess you would have to convert it somehow.

Is there a way to prevent Formtastic from overriding "type" parameter?

I am using a jquery plugin for a datetime picker, and it requires the type parameter to be set to 'text'.
So I tried the normal method:
= semantic_form_for #schedule do |f|
= f.input :start_at, :as => :datetime_picker, :input_html => { :class=> 'datetimepicker', :type => 'text' }
But Formtastic is overriding that type and setting it to datetime-local.
<input id="datetimepicker" maxlength="16" name="schedule[start_at]" size="16" step="1" type="datetime-local">
Is there a way to negate Formtastic automatically setting the type, without changing the gem?
So, I realised that I was heading down the wrong path and that if I wanted it be of the text type, it should be a string or text input. Which went against my original thinking that if I want a datetime picker, I should tell formtastic, when I should have only been telling the jquery plugin.
Here is what my haml looks like now:
= semantic_form_for #schedule do |f|
= f.input :start_at, :as => :string, :input_html => { :class => 'datetimepicker', :type => 'text' }

Rails jquery datepicker date pre-loaded

I'm using jquery datepciker in a Rails app.
I would like to have the date already filled in when the form page loads. So, that the user can leave the date as-is or change it.
I was hoping the datepicker options would let me do that.
I tried this:
$("#comment_status_date").datepicker({
dateFormat: 'yy-mm-dd',
setDate: Date.now
});
But, the date field was empty.
Do I have to do some ruby code in the model or controller?
Does datepicker have an option to do this?
Thanks!!
I figured out a way:
<%= f.input :status_date, :as => :string, :label => 'Date', :class => 'calendar hasDatepicker', :input_html => { :value => Date.today.to_s } %>
I figured out a way:
<%= f.input :status_date, :as => :string, :label => 'Date', :class => 'calendar hasDatepicker', :input_html => { :value => Date.today.to_s } %>

simple_forms custom data attribute

I would like to have an additional data attribute on an input tag generated by simple_form.
The following does not work:
<%= f.input :date, :as => 'date_picker', :data => {:datepicker => :datepicker} %>
How could this be done? Is it possible at all? As you might have guessed: I am trying to add bootstrap-datepicker to my site without using explicit js to initialize the date picker.
The correct API is:
f.input :date, :as => 'date_picker', :input_html => { :data => {:datepicker => :datepicker} }
For prosperity. On Rails 4.2.5 and Simple Form 3.2.1
The above from #rafaelfranca works for inputs:
f.input :first_name, input_html: { data: { cool_stuff: "yes" } }
If you want to add a data attribute to the simple_form_for helper:
simple_form_for #form, html: { data: { super_cool_key: "secret" } } do |f|
Note the difference: input_html: vs html:
Also, the underscores will automatically get changed to a dash.

Resources