Rails Hours:Minutes Dropdowns - ruby-on-rails

I'm new to Rails and I want to create a simple time calculator:
User is presented with 2 dropdown menus that capture user inputs “hour” and “minutes”, so I can get time like 7:15. Then after some math on this time the user is given several “hour:minute” time options. What is the best way to build this logic and what helper to use? Should I create models for hours and minutes?

You can build up the hour and minutes drop down.
<%= f.select :hours, '1'..'24' %>
<%= f.select :minutes, '01'..'59' %>
or you can use gem

You can use time_select to get hours and minutes from user.

Related

Multiple days of week in one attribute in database

I want to implement a feature in Rails using the option of multiple checkbox. User must be able to choose the days in which he want the emails, like Monday, Tuesday, etc. This must be a list of checkboxes with day name as label and day index like 0 for Sunday, 1 for Monday, like that.
Also these fields must be checked when the user next time come here to edit.
I don't want to create separate db field for each day. This can be an array of day index stored in one database field. I am using Rails version 4.
Date::DAYNAMES will give the list of week days. Date::DAYNAMES.each_with_index will give the list of days with its index. I want to know the best way to implement the same.
Try this,
<%=select_tag 'days[]', options_for_select(Date::DAYNAMES.zip((0..6).to_a),
[selected days array goes here]
), :multiple => true%>
For using checkbox
<ul>
<% Date::DAYNAMES.zip((0..6).to_a).each do |day| %>
<li>
<%= check_box_tag 'days[]', day[1], [selected days array].include?(day[1]) -%>
<%= h day[0] -%>
</li>
<% end %>
</ul>
Hope it's help you.

Create multiple entries in model through nested form via simple_form

When a user creates a vacancy I want them to be able to save either 1 range of dates or multiple separate dates.
So I have 2 models, Vacancy and Vacancyschedule.
Vacancyschedule includes vacancy_id start_date end_date start_hour end_hour (to save a range) and when it is multiple seperate dates, I just want to leave the end_date empty and have multiple entries and combine them through my vacancy_id.
This is my code in my view:
... other code to ask for vacancy params, nothing special, nothing broken ...
#Code to create entry in vacancyschedule
<%= t.simple_fields_for :vacancyschedule do |p| %>
<%= p.input :start_date, :as => :date_picker, :label => false%>
<%= p.input :start_hour, as: :time, default: Time.parse('09:00')%>
<%= p.input :end_hour, as: :time, default: Time.parse('17:00')%>
<% end %>
And then I have some javascript that adds another exact copy one of those blocks when a user wants to add a second seperate date.
Now for my question:
The format in which they are passed is very strange. As you can see I ask for :start_date, :start_hour and :end hour and this is what I get:
{"name"=>"", "street"=>"", "description"=>"", "skill_id"=>"",
"jobtype_id"=>"", "wage"=>"", "vacancyschedule"=>
{"start_date"=>"27/10/15", "end_date"=>"", "start_hour"=>"",
"end_hour"=>"", "start_hour(1i)"=>"2015", "start_hour(2i)"=>"10",
"start_hour(3i)"=>"26", "start_hour(4i)"=>"21",
"start_hour(5i)"=>"00", "end_hour(1i)"=>"2015", "end_hour(2i)"=>"10",
"end_hour(3i)"=>"26", "end_hour(4i)"=>"17", "end_hour(5i)"=>"00"},
"vacancyscheduele"=>{"start_date"=>"", "start_hour(1i)"=>"2015",
"start_hour(2i)"=>"10", "start_hour(3i)"=>"26", "start_hour(4i)"=>"09",
"start_hour(5i)"=>"00", "end_hour(1i)"=>"2015", "end_hour(2i)"=>"10",
"end_hour(3i)"=>"26", "end_hour(4i)"=>"17", "end_hour(5i)"=>"00"},
"tag_list"=>""}, "radio"=>"on", "commit"=>"Create Vacancy",
"controller"=>"vacancies", "action"=>"create", "employer_id"=>"2"}
From what I can see is that they are passed in very different variables and incomplete (not all dates are passed).
Does anyone have any experience with this issue? How would I either grab these elements or how do I prevent them from being pushed in that format?
Eeeehhh, I hate nested forms. I always go back to the basics when I have to deal with them. Checkout these fine railscasts. The second one I think is better aimed at your problem but the first one helps understand it:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
At the end of the day, you need to know what your html is generating and make sure it is what Rails is expecting in the backend. Also, you must account for the nested objects at load and save time. Your HTML as it stands, is not generating parseable objects as far as Rails is concerned. On the other hand, it is hard to deal with the crap date/time picker for Rails. I usually like to put in something that generates a better date picker. One that generates one string that is easily maps to a database field. If you are using bootstrap, I'd use the bootstrap datepicker gem.

Creating a date range picker using two select boxes in Rails

I'd like to setup a model that would allow a date range depicting a timeframe of activity-inactivity. So for example you have an employee model and would be able to set his/her duration of employment from a start date to an end date. Essentially there would be two select boxes that would allow you to do this. If the employee is still employed there would be an option in the second box labeled "current".
I've looked around for an existing answer to this problem and it seems most direct you towards using either date_select or select_year to create a range within one select box. I'm looking to do something of the sort using two select boxes (start / end || current) and saving the two values to the database. Now for the second select box I wanted to have it default to the current year and be called "Current", indicating there is no end date yet.
Looking through the Rails API there's an option for a prompt but I'm not exactly sure how to have that prompt represent a physical value and reside at the top of the list. For something as simple as this I was leaning towards not using an extensive jQuery datepicker plugin to reduce the unnecessary overhead. I'm open to using SimpleForm but haven't found a way to do this through that gem.
What I have now:
<% form_for #product do |f| %>
<%= f.label :employed %>
<%= select_year(Date.today, start_year: 2000, end_year: 2012) %>
<%= select_year(Date.today, :start_year => Date.today.year, :end_year => 8.years.from_now.year) %>
<% end %>
I'm wondering if creating two attributes to the employee model specifying these date (stardate + enddate) would work or could you possibly do this in one fell swoop (I'm assuming the latter would be cleaner)?
I would consider creating a separate table to store these values, call it EmploymentPeriod. An Employee then has_many EmploymentPeriods, which could be useful to keep track of... (maybe an employee is a student, and works one summer, and then returns the following, for example).
EmploymentPeriod could keep track of things like start_date, end_date, and even something like salary, for that particular period (maybe the student gets a raise the following summer...).
To recap:
Employee has_many :employment_periods
EmploymentPeriod belongs_to :employee # e.g. it has an employee_id foreign key
This is how I would tackle it! Good luck.

Rails Form display datetime at local timezone

I'm new to rails (just finished the Rails Tutorial) and I'm building my first solo app. The user can create 'events' at specific times. Currently the user form is in UTC. How do I set it to their local time?
I don't just want to change it after they submit, because it would be confusing for the user. When you open a new form the default time is the current time (currently UTC). I want that to be the time of their local timezone.
<%= f.label :Date_Time %><br />
<%= f.datetime_select :date %>
thanks,
Loads of ways to do this!
I'd recommend you look at the question Stepan recommended first
Server Side
If you're wanting to keep your times consistent, you may wish to use the Rails in-built timezone option:
#config/application.rb
config.time_zone = 'Eastern Time (US & Canada)'
You can see more about ActiveSupport::Timezone here
Client-Side
Client-side, you'd have to think about what you want to achieve. If you have set timezones, you may benefit from creating a TimeZone repository (either ENV variables or datatable), and allow user to select from them, like this:
#app/views/form.html.erb
<%= form_tag %>
<%= select_tag :date, options_from_collection_for_select(#timezones, "id", "name") %>
<% end %>
Alternatively, you could pass the timezone through javascript: Getting the client's timezone in JavaScript
UX
The bottom line is you need to think about what you're trying to achieve. If your users need localized times, you should use a country-select system, where they will give you their country, and you can dedicate a timezone
Speaking from experience, it's much better to store all times equally (using UTC), and then process them using user's specifications

Rails Date Select Parameters

I'm using a date select in a rails 3 form.
<%=f.date_select :date %>
I would like to restrict the dates so that you can only pick dates that fall on a Sunday. Is there any way of going about doing this?
I'm also trying to stop dates which have already passed from appearing.
Thanks for any help in advance!
Rails date_select field generates three dropdown to select the parts of the date. There is no chanche, that you modify for example the month, and the day will still be sunday.
You must write some js magic to enforce such a role, or find an already existing datepicker and limit it. Or alternatively, you let the user to select a week, and calculate the exact date of sunday from that.
Ok having studied this out a bit further I don't think this is possible due to the format of the date_select field. The closest I can get is
<%=f.date_select :date, start_year: Time.now.year %>
so that at least you can't select dates from previous years. I've implemented the restriction on days and months that have past by setting up the view to automatically delete records that aren't relevant:
<% if(service.date < Date.today) %>
<% service.destroy %>
<% end %>
Not perfect but does the job in my case.

Resources