Rails jquery datepicker date pre-loaded - ruby-on-rails

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 } %>

Related

Rails Make Form Text Field Show Value From Model or Default Value

Rails 5.0.4 ruby 2.5.1p57
I have a form partial that has a date field. It uses the datepicker class.
Currently, I have it set to display the current-date:
<%= form_for(#weight) do |f| %>
<%= f.text_field :workout_date, { :size => 20, :type => 'date', :class => "date-picker", :readonly => false, :value => Date.today } %>
Because I use this form/partial on both the new and edit views, I would like it to show either the date in the model (for an edit) or the current date (for a new record).
I tried this:
<%= f.text_field :workout_date, { :size => 20, :type => 'date', :class => "date-picker", :readonly => false, :value => #weight.workout_date.present? ? #weight.workout_date : Date.today } %>
But, this displays 09/01/2019, when viewing the new form today 09/12/2019.
Ideas on what I'm doing wrong? Can this be done?
Thanks for any tips.
I think I figured this out. In my migration to create the weights model:
t.date "workout_date", :default => Date.today I created this on 09/01/2019, so the form is using this date.
<%= form_for(#weight) do |f| %> I'm passing a weight object for a new record. It must see that default date and use it.
Fix: create a migration to removed that default date. OR, since the new model record does not have an ID, I can do this cheap-hack, which works, until I fix the model:
<%= f.text_field :workout_date, { :size => 20, :type => 'date', :class => "date-picker", :readonly => false, :value => #weight.id.present? ? #weight.workout_date : Date.today } %>
I also do not need the .present?. It works without it.

Simple_form input as both text and autocomplete

I am using gem 'simple_form', '2.0' and gem 'rails3-jquery-autocomplete', '1.0.10'.
I am unable to make a string field accept two ':as' arguments.
<%= f.input :product_description , :url => autocomplete_product_name_products_path,
:as => :text, :as => :autocomplete, :placeholder=>"Type product name",
:input_html => {:class =>"span2", :rows => 6}, wrapper: :inline_label, label:false %>
If I remove :as => :text, the auto-complete part works but ':rows => 6' part fail.
And if I keep both, I get 6 rows but the auto-complete stops working.
I need this field to have have multiple rows as well as auto-complete.
This is a really silly question but I am stuck. Please help.
Simple Form doesn't define an autocomplete input. But if you need an autocomplete input and the text type I think you can do:
f.input :product_description ,
:url => autocomplete_product_name_products_path,
:as => :autocomplete,
:placeholder=>"Type product name",
:input_html => {:class =>"span2", :rows => 6, type => :text },
wrapper: :inline_label, label: false

Simple form format a time field to show '21:30' instead of entire UTC time string

My rails _form code:
<%= simple_form_for #admin_event, :html => { :class => 'main-form' } do |f| %>
<%= f.input :time_of_event, :format => l(:time_of_event, '%d %b. %Y'), :as => :string, :label => 'Hora', :input_html => { :class => 'thin', :placeholder => 'eg: 20:30' } %>
<% end %>
I'm getting the error message:
Cannot convert symbol to string.
How can I set up this _form to always display this field using a time converter so while the database has a full field (2000-01-01 19:30:00.000000), in the forms it would only show 19:30?
Solved this with the following: created the method "time_format" on my ApplicationHelper:
def time_format(datetime)
datetime.strftime('%H:%M') unless datetime.blank?
end
And then on the form:
<%= f.input_field :start_time, :as => :string, :value => time_format(f.object.start_time) %>
Hope this will help you.
You can add attr_accessor to your model (for example formatted_time) and use this field for get fromatted time and set it. Before save you can parse value of #formatted_time and apply it to time_of_event field.

Rails simple-form datepicker how to add data-behaviour

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" }

How to refresh the web page when user select a particular value on dropdown list on rails?

I'm developing project on rails 2.3.8 and I need to refresh the whole web page when user select particular selection on drop-down menu. How can I do it on rails ?
This is my drop down menu
<%= collection_select("country", "id", #countries , :id, :name, {:prompt => true}, :id => 'xx') %>
<%= observe_field('xx', :url => { :controller => 'calendar', :action => 'update_country' },:update => 'app_header',:with => "'con=' + escape(value)")%>
This finely load the countries so how I can reload the whole page ? please can some one explain me about this?
Simply add a html option to your collection select , onchange => "Javascript code to reload the page"
<%= collection_select("country", "id", #countries , :id, :name, {:prompt => true}, :id => 'xx', :onchange => "location.href = '#{root_url}'") %>
Or you can just refresh the current page:
<%= collection_select("country", "id", #countries , :id, :name, {:prompt => true}, :id => 'xx', :onchange => "location.href = window.location.href") %>

Resources