How to Modify the Input Type of the Rails Datetime_Select Helper

How can I modify the input type of the Rails datetime_select helper?

how about rolling your own simple helper, like

def datetime_text_fields(object_name, method)
html = text_field_tag("#{object_name}[#{method}(3i)]", Date.today.day.to_s, :length => 2)
html << select_month(Date.today, :field_name => "#{object_name}[#{method}(2i)]")
html << text_field_tag("#{object_name}[#{method}(1i)]", Date.today.year.to_s, :length => 4)
html << " "
html << text_field_tag("#{object_name}[#{method}(4i)]", Time.now.hour.to_s, :length => 2)
html << ":"
html << text_field_tag("#{object_name}[#{method}(5i)]", Time.now.min.to_s, :length => 2)
end

Feel free to add more formatting stuff/separators etc. but it basically returns to correct field names for rails to be identified as DateTime. Rails expects fields named like date(1i), 1i = year, 2i = month etc.

Honestly I didn't test it or anything, but the output in console looked pretty convincing ;)

Override Rails date_select form helper

ActionView::Helpers::Tags is new in Rails 4 (you link to the rails master branch, which is the 4.0 branch), where you are working on a Rails 3.2.x app, you need to look at the source code for that 3.2stable branch. This looks like the place to monkey patch date_select

Get Rails Date Select Form Helper to display on a single line

Have you tried

float: left;
width: 150px;

The width has to be fixed with float left to make elements "follow" each other.

How to work with date_select form helper without a model in Rails?

Here's what finally worked:

<% @the_date = Date.strptime(@the_date_string, "%Y-%m-%d") %>
<%= date_select("the_date_string", "", :default => @the_date) %>

I am storing the date as a string. So, it needs to be converted to a date object before displaying in the HTML form, and converted back to a string before saving in the database.

Rails 4 x Bootstrap 3: integrating datetime picker with datetime select helper

Here is what I ended up doing, with the precious help of Dave Hulihan himself.

In config/initializers/form.rb:

module ActionView
module Helpers
class FormBuilder
def date_select(method, options = {}, html_options = {})
existing_date = @object.send(method)
formatted_date = existing_date.to_date.strftime("%F") if existing_date.present?
@template.content_tag(:div, :class => "input-group") do
text_field(method, :value => formatted_date, :class => "form-control datepicker", :"data-date-format" => "YYYY-MM-DD") +
@template.content_tag(:span, @template.content_tag(:span, "", :class => "glyphicon glyphicon-calendar") ,:class => "input-group-addon")
end
end

def datetime_select(method, options = {}, html_options = {})
existing_time = @object.send(method)
existing_time ||= Time.now
formatted_time = existing_time.to_time.strftime("%A, %B %d %Y %I:%M %p") if existing_time.present?
@template.content_tag(:div, :class => "input-group") do
text_field(method, :value => formatted_time, :class => "form-control datetimepicker", :"data-date-format" => "dddd, MMMM DD YYYY hh:mm A") +
@template.content_tag(:span, @template.content_tag(:span, "", :class => "glyphicon glyphicon-calendar") ,:class => "input-group-addon")
end
end
end
end
end

And then, in app/assets/javascripts/posts.coffee:

$(document).ready ->
$('.datetimepicker').datetimepicker()

Now, the datetime picker is working like a charm.

Rails 4 How Do You Make 'date_select' Form Field Helper Required?

The required is a html option so it's a separate hash from the other options. Use curly braces to surround the first hash so that the required is clearly in the second hash.

<%= f.date_select :birthday, {order: [:month, :day, :year], prompt: { day: 'Select day', month: 'Select month', year: 'Select year' }, start_year: Date.today.year - 18, end_year: Date.today.year - 100}, {required: true} %>


Related Topics



Leave a reply



Submit