Rails bug? Model form with datetime_select can't set default - ruby-on-rails

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?

Related

Trying to default rails time_select to blank values

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.

Date in Rails form

I have a form field in my Rails view:
<%= f.date_select :Date_of_Birth %>
This shows a drop down menu with only the last 10 years and I need to go back to 1800. Better if I could just type the year in rather than have a 200 item drop down list. I also need the option not to fill in the date, that is, blank.
I have found a couple of hints here in stackoverflow, but they don't use quite this format which was generated for me. Suspect because I'm using Rails 4.
Here's how to extend the range of selection:
<%= f.date_select :Date_of_Birth, start_year: 1800, end_year: Time.now.year %>
Having a text input field for the year is not so easy but it could be done, by adding discard_year: true to the date_select.
Then you add a text field tag AFTER the date_select inputs with a special name like name_of_your_object_Date_of_Birth(1i) where "name_of_your_object" is the name you used in the form_for tag. Inspect the HTML fields which date_selects generates in your browser.

How to set default date and include_blank in date_select in Rails

I'm using date_select in Rails 4 and I'd like to be able to have today's date as default but also allow for the user to leave the date field blank.
<%= f.date_select(:birthdate, {include_blank: true, default: Date.today.to_date, start_year: Date.today.year - 100, end_year: Date.today.year - 18}) %>
What is the best way to allow for blank as well as have today's date appear by default when the page opens?
The option you need is selected, i.e:
<%= f.date_select(:birthdate,
include_blank: true,
selected: Date.today, # substitute 18.years.ago to prefill year
start_year: Date.today.year - 100,
end_year: Date.today.year - 18) %>
The approved answer will overwrite the value already assigned to the birthdate every time you go to edit the record. See here.
If your model is, e.g., #student, and your form is form_for #student do |f|, this should work. It fails through to Date.current if #student.birthdate is empty.
<%= f.date_select(:birthdate,
include_blank: true,
selected: #student.birthdate || Date.current, # Date.current respects time_zones
start_year: Date.today.year - 100,
end_year: Date.today.year - 18) %>
However, be aware that the blank option will always be set to the current date when you go to edit the record, requiring the user to reset to blank if the data is unknown. This risks the insertion of bad data - arguably, less usable than having to choose the date to begin with.

In simple_form, can't submit boolean as radio_button if the field is disabled

I am using simple_form 2.0. I have a Boolean field 'stock' which I am trying to submit as radio buttons.
<%= f.input :stock , :as => :radio_buttons, :collection => [['Purchase Indent', false],
['Stock', true]], label:"Shipments From" , :disabled => true%>
The stock is marked as false before rendering the form.
Once I submit the form the stock itself is missing from the parameter and I get this error.
Because I am validating stock's inclusion.
validates_inclusion_of :stock, :in => [true, false]
It works fine if i don't disable the field. But I don't want user to be able to change it.
Please help.
Update
The reason is that, the disabled fields are never sent. http://www.w3.org/TR/html401/interact/forms.html#h-17.12
Seems like making it read-only will help.
https://github.com/plataformatec/simple_form/pull/367
But, the news is radio buttons can't be made read only.
Why can't radio buttons be "readonly"?
One option is to separate the buttons and only disable the unselected option:
<%= f.input :stock , :as => :radio_buttons, collection: [['Purchase Indent', false]], label:"Shipments From" %>
<%= f.input :stock , :as => :radio_buttons, collection: [['Stock', true]], label:"" , :disabled => true %>
Another option would be to add a hidden input with the desired value.
Remember not to trust user submitted data.
I don't think you should build it like this, because a hacker can just change the HTML / submit an artificial request even if you disable the form elements. Hidden form elements don't fix this, as anyone with a dom explorer can change the values. Of course, if your model checks and rejects this kind of thing it's not such a big problem.
So to fix the particular problem, just do the visuals as you have already, and re-insert the expected value in your controller's update or create action.
For more info there's lots online e.g. owasp, but I liked the book "How to break web software" from a few years back by some guys at Google.

Rails - how to set default on edit form for a year select form

In my app, I let people select their start year for a job. This works fine when creating a new job, but on the edit form I want the year the user selected during create to be automatically population.
My code:
<%= select_year Date.today, start_year: Time.now.year, end_year: Time.now.year - 95, field_name: :start, prefix: :job %>
Currently, it just defaults to 2013 everytime. I know I am passing in the instance variable correctly because the other fields are populated automatically, just not this year dropdown.
How do I get this to work?
This answer was too long to put in the comments, so I'm putting it here as an answer, but let me know if it answers your question or not.
You could manually set the start_year and end_year variables to, for example, 1950 and Time.now.year. Then, set the first argument in select_year helper to the instance variable to make it the default selection between the wide range instead of Date.today.
For example:
<%= select_year #instance_variable, start_year: 1950, end_year: Time.now.year, field_name: :start, prefix: :job %>
More examples are available in the Rails Docs, too:
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_year

Resources