Trying to default rails time_select to blank values - ruby-on-rails

Ride is a model that has an attribute called :requested_time which is a datetime. I want site users to be able to pick their time but not the date. I am trying to make a time select dropdown that defaults to a blank value. A blank value is appearing in the form, but it is defaulting to 5AM and I am not sure why. I want the default to be blank. Here is the view code:
<%= f.time_select :requested_time,
{twelve_hour: true, minute_step: 15, ampm: true, include_blank: true},
class: 'form-control pull-left', style: 'width: 10%; margin-right: 10px;' %>
What can I put in the view code to make the field default to blank values?
To illustrate my problem, here is what shows up:
If I click on the field, a blank value is there:
I want that blank value to be what the select field is automatically set to when the page load.

I believe in the controller you have to some explicit checks like this:
if params[:start_time] && ! params[:start_time][:hour].blank? && !params[:start_time][:minute].blank?
params[:event][:start_time] = "#{params[:start_time][:hour]}:#{params[:start_time][:minute]}:00"
else
params[:event][:start_time] = nil
end
To confirm check to make sure that the browser is sending blanks up as the start_time, if it is then the above should work.
BTW the reason you are getting 5AM I am pretty sure is because of time zone issues. Use Time.zone to set your zone properly.

Related

Rails time_select avoid pre-filled and storing on ddbb

my time select is being pre-filled with the current hour:
<%= form.time_select :afterstart, class: "form-control" %>
How can I have a blank placeholder (default to null) so it doesn't get stored in ddbb unless the user fills it in?
Couldn't find any usable method in the API docs.
Thanks!
you can use include_blank: true to avoid pre-filled
<%= form.time_select :afterstart, include_blank: true, class: "form-control" %>
but the time_select helper will automatically add default time, so the attribute afterstart of your model still be saved to db, to avoid this, you can merge your model params with blank afterstart(1i) to afterstart(3i)
# your-model controller
private
def your_model_params
params.require(:your_model).permit(...,:afterstart).merge!({
"afterstart(1i)": "",
"afterstart(2i)": "",
"afterstart(3i)": ""
})
end
so that if user does not select time, that mean afterstart(4i) and afterstart(4i) blank -> your model afterstart will be nil and not to be saved to db.
another way is to check if 4i && 5i blank then reject all params keys start with afterstart.

Disabled Haml field is not passed to my rb file

I have a single form which has some input fields. This form, firstly get values from database and fill them in. If they have value they will be disabled.
=text_field_tag("dt|#{a_key}",(begin;a_mock["dt_open"];rescue;nil;end), :class => "form-control", :disabled => a_mock["active"] == "true" )
In my rb file there is a rule which validate date. If the field is blank:
if !dt_open.blank?
...
end
What I noticed is if the field are disabled, it doesn't send. I know because I print the message and the field aren't there.
How can overcome this behavior in haml?
Input with disabled attribute will not submitted, you can user readonly rather disabled, did the almost same thing you can not change or edit input field value.
Another solution
If you still want to use the disabled attribute the you can apply the following javascript to allow submit the disabled
<script>
$('#your_form_id').submit(function() {
$("#disabled_input_field_id").prop('disabled', false);
//your code goes here
})
</script>
You can use a hidden_field_tag that will post the value on submit. I would also just print the value in a p tag instead of showing a disabled text input, but that is just my opinion.
- if a_mock["active"] == "true"
= hidden_field_tag "dt|#{a_key}", a_mock["dt_open"]
p= a_mock["dt_open"]
- else
= text_field_tag "dt|#{a_key}", a_mock["dt_open"], class: "form-control"

How to include blank but disable it for simple form collection select?

So I need (for UX reasons) to include a blank field in select field (with include_blank: option), but disable it.
Is this possible to do?
I've been trying to pass
disabled: [nil]
and
disabled: [""]
But it does not seem to cut it..
This is not possible. However you could use a combination of prompt/include_blank and required to ensure the end-user selects a proper value:
<%= select_tag('foo', options_for_select([['bar','bar'],['baz','baz']]), prompt: "Select a Foo", required: true %>
or
<%= select_tag('foo', options_for_select([['bar','bar'],['baz','baz']]), required: true %>
include_blank is automatically set to true when you set the required option to true and no prompt is present.
I am having a similar issue with filtering users by accepted insurance plans and cannot make the selection required (if none selected, show all users), so I added something like this to the controller (maybe hacky, but ensures a blank default first option that will be ignored unless the user selects another):
User.joins(:insurances).where(insurances: { id: query[:insurance] }) unless query[:insurance].first.empty?
where query[:insurance] returns [""] if none selected.

Rails bug? Model form with datetime_select can't set default

Has anyone come across this? I can't set the defaults for my time with the form while it is associated with my model
e.g.
<%= f.datetime_select :date_start, { default: #event.date_start.in_time_zone(current_user.time_zone), ampm: true, use_short_month: true, start_year: Time.now.year, end_year: Time.now.year+1, minute_step: 5}, class: 'form-control form-control-date' %>
The reason I need defaults because it defaults to utc. I need the edit form to factor in the time zone otherwise the user will see the wrong time they entered.
As it is, it ignores the default completely and leave it in UTC. This is an issue in the edit form.
If I change it to a tag, the default works. However I would need to change all my forms to tag so the form submits properly.
Anyone have a fix besides possibly doing that?

How do i tell the check_box method to NOT add a hidden check_box for the 'unchecked' value?

I'm using rails 2.3.4, which, when you call .check_box on the 'f' object in a form_for, makes a visible checkbox input for the 'checked' value, and a hidden checkbox input for the 'unchecked' value: http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002434&name=check_box
The problem with this is that i have a validates_acceptance_of validation on the check_box, and if it's not checked, i'm getting a field_with_errors div wrapped around the visible checkbox AND the hidden checkbox, so that the error message appears twice.
In this instance i don't want a value passed through in the 'unchecked' case, so i don't want rails to add the hidden checkbox - this (switching off the hidden checkbox) would solve my problem. I can't figure out how to tell it to not add the hidden checkbox though. Can anyone tell me?
I know that i could get round this by making a check_box_tag, which doesn't add the hidden 'unchecked' case checkbox, but then i don't get field_with_errors stuff wrapped around the checkbox if it's not checked. Dispensing with the hidden field seems like the cleanest solution.
Thanks - max
In Rails 4 you can use include_hidden: false
example f.check_box :some_field, include_hidden: false
Since Rails 3.2, the hidden field will not be shown if the unchecked_value argument evaluates to false.
Example: f.check_box :your_field, {}, checked_value, false
See the Rails source: 3.2,
4.1
use <%= check_box_tag "your_model[your_field]" %>
f.check_box always gives you a hidden field.
So this is interesting. check_box takes four arguments as the method definition shows:
def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
Tags::CheckBox.new(object_name, method, self, checked_value, unchecked_value, options).render
end
The checked value and unchecked value must be the third and fourth argument. By setting an unchecked_value that evaluates to a truthy value, then the hidden input will appear.
My initial goal was to have a visible checkbox with a value of true, and a hidden checkbox with a value of false. So I tried doing this:
<%= f.check_box :share, { }, true, false %>
However, because I actually passed in a boolean value for unchecked value, it made the hidden field not appear at all! I tracked it down to this line of code in the Rails source:
def hidden_field_for_checkbox(options)
#unchecked_value ? tag("input", options.slice("name", "disabled", "form").merge!("type" => "hidden", "value" => #unchecked_value)) : "".html_safe
end
If #unchecked_value evaluates to false or nil, then no hidden input field would display. Now if you want to send true and false values rather than '1' or '0' using check_box helper, then you will need to wrap the booleans in strings:
<%= f.check_box :share, { }, 'true', 'false' %>
But if that's the case, you matters well just use the defaults of '1' and '0' for boolean values and let Rails handle it.
Rails stores booleans as TinyInt, at least for the MySQL database. Consequently, that '1' will get stored in the database as 1 and will be considered a true value.

Resources