I only want certain hours on my rails time_select form - ruby-on-rails

In my app, users should only be able to select times between 10am and 12pm, and then 4pm and 6pm. Basically I want my 'hours' drop-down menu to only list "10am, 11am, 12pm" and "4pm, 5pm, 6pm". Is this possible?

<%= f.time_select :time,
{ampm: true,
ignore_date: true,
start_hour: 10,
end_hour: 18,
discard_minute: true,
prompt: true,
prompt: { hour: "Choose hr" }
} %>
can be a start? may be you can write a script to disable the other hours or you use JavaScript to genereate the html options for you?

Related

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.

use Rails date_select with Angular

How do you use Rails date_select form helper and generate a seperate ng-model for month and for year?
// Slim
= f.date_select :from, {
order: [:month, :year],
start_year: Time.now.year,
end_year: 1990,
include_blank: true },
{ :'ng-required' => 'isRequired'
}
The date_select makes it easy to set differenct prompts per each field. Is there any way to do this with your custom attributes?
date_select("article", "written_on", prompt: { day: 'Select day', month: 'Select month', year: 'Select year' })
The problem is that date_select creates 3 select_tag fields(one for month, year, and day). You can add the ng-model tag within html_options for date_select, but the problem is that all 3 select fields will have an identical ng-model tag with the same name. Angular doesn't like having multiple ng-model objects with the same name, and I don't know how to get around this. The solution I used was just to recreate what date_select does and create 3 select_tag fields. It's definitely not as elegant as the date_select field but it does work. I'd love to hear if anyone has a better solution!

Rails - how to validate one field against another for dates

I think this is a simple question. In my app, I have a model where users select a 'start' date and a 'stop' date when submitting a new record. The dates are only the years, excluding month and day.
I just want to write a validation to ensure that the stop date is greater than the start date, so you can't submit something that started in 2012 and ended in 2008. How would I do that?
My form fields are below:
<%= select_year Date.today, start_year: Time.now.year, end_year: Time.now.year - 95, field_name: :start %>
<%= select_year Date.today, start_year: Time.now.year, end_year: Time.now.year - 95, field_name: :stop %>
If you want to do a server side validation, you could add validation to your model
validate :stop_date
def stop_date
errors.add(:stop, "stop date cannot be older than start date") if stop < end
end
You could populate the errors on the view.

How to stop rails from including current day and month in date helper

I'm trying to use Rails date helpers with a form. However, I only want to get the year. I got the form code below from simple_form gem https://github.com/plataformatec/simple_form. The discard_day and discard_month prevent the month and day select box from showing. However, when I submit the form, I'm getting this multiparameter error
2 error(s) on assignment of multiparameter attributes
"start(1i)"=>"2013",
"start(2i)"=>"4",
"start(3i)"=>"23",
"end(1i)"=>"2011",
"end(2i)"=>"4",
"end(3i)"=>"23",
It appears Rails is just substituting the current day and month since none was entered in the form.
I'm wondering if there's a way to prevent Rails from including the current day and month, or am I supposed to deal with it in the controller. Right now, the create action looks like this
def create
#employment = current_user.employments.build(params[:employment])
if #employment.save
....
If I can't stop Rails from including the current day and month, then what would I do in the controller?
<%= f.input :start, as: :date, start_year: Date.today.year - 90,
end_year: Date.today.year, discard_day: true, discard_month: true %>
<%= f.input :end, as: :date, end_year: Date.today.year - 90,
end_year: Date.today.year, discard_day: true, discard_month: true %>
What is your data type for the start and end columns? If you are just storing a year, you can set them to an integer type instead of a date, and use <%= f.input :start, collection: (Date.today.year - 90)..(Date.today.year) %>.

Disable Previous Months in date_select Ruby on Rails

I want to create a month drop down list so that the previous months of the current year do not appear.
Right now my code is :
<%= f.date_select :card_expires_on, :discard_day => true, :start_year => Date.today.year, :end_year => (Date.today.year+10), :add_month_numbers => true %>
it shows me all the months now, but I want to remove or hide the past months..Is there a way?
Thanks in advance! :-)
I haven't tested this but I think it should work.
<%= select nil, "name_of_your_model[card_expires_on(1i)]", (Date.today.year..Date.today.year+10).collect { |y| y }, options = {}, id: "card_expires_on_1i" %>
<%= select nil, "name_of_your_model[card_expires_on(2i)]", (Date.today..Date.today.end_of_year).collect { |d| ["#{d.strftime('%m')} - #{d.strftime('%B')}", d.strftime('%m')] }.uniq, options = {}, id: "card_expires_on_2i" %>
<%= text_field_tag "name_of_your_model[card_expires_on(3i)]", nil, type: 'hidden', value: Date.today.day, id: "card_expires_on_3i" %>
Basically when you put date_select in your form it creates three selects. Rails then uses the "(1i)", "(2i)", and "(3i)" to bring the year, month, and day, respectively, back together. Make sure you change "name_of_your_model" to the name of the model used for the form and the value of the text_field to what ever default day you want.
Update
I just tested this and it did work!
Seems to me like you should be able to add this to a helper. I'd shy away from javascript
adding your own method for remaining_months() or some such thing would be much simpler to use.

Resources