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.
Related
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.
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.
I have a column DATENEW in my invoices table.
If in my view I use:
<td><%= invoice.DATENEW %></td>
it shows:
2015-02-16 11:38:03 UTC
I need to display only year month and day.
How can I do it?
You can use strftime to show only year, month and date.
<%= invoice.DATENEW.strftime('%Y-%m-%d') %>
And for grouping, first show us what you've tried.
These Time / DateTime methods also may come in handy:
at_beginning_of_month
at_beginning_of_day
at_beginning_of_year
at_beginning_of_week
at_beginning_of_hour
at_beginning_of_minute
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.
In my application a user can fill in a form to create learning goals for the classes they take each semester at school. This application should guide them through their full 4 years of their study.
In my form I've included
<p><%= Time.now.year %>/<%= Time.now.year + 1 %></p>
to get the current schoolyear. In the index of their learning goals is a table which lists the schoolyear in which their goals were created.
How do I get it to show 2013/2014 (the schoolyear in which it was created) when it's, for example, the year 2015?
If each goal has a created_at field, it should be as simple as using this for each goal:
<%= goal.created_at.year %>/<%= goal.created_at.year + 1 %>
you have to save these values in database, so that u can retrieve them anytime, or you can simply use created_at date of the table(example:- goal.created_at if goals is the table)