Rails non-table drop down list - ruby-on-rails

I need to have a drop down list in which the user selects the day of the week they want to come in every week. The values will never change right. It's just Sunday, Monday, ..., Saturday right? It seems like more work than needed to make a table and put the days of the week in. I certainly don't need the ability to create, update or delete them. Is there a simple way to handle something like this? Or maybe instead of days of the week it could be status like off, park, reverse, neutral, drive. The main thing is that the values are never going to change. There are just a few of them. So why make a table? I am thinking that there is a way to create a model that already has data in it but I could be wrong.

Why create a model? Just use select.
DAYS = ['Monday', 'Tuesday', 'Wednesday', ...]
select(:event, :day, DAYS)
It's usually better practice to place the constant in the relevant model and use it from there.
In your model:
class Event < ActiveRecord::Base
DAYS = ['Monday', 'Tuesday', 'Wednesday', ...]
end
and then in your view:
select(:event, :day, Event::DAYS)
and here's another trick I use a lot:
select(:event, :day, Event::DAYS.collect {|d| [d, Event::DAYS.index(d)]})

Note that Ruby has the English names for the days of the week already built into its date class. You should try to leverage that if you can. Here's the rdoc.
Then as Can has suggested just do the following:
select(:event, :day, Date::DAYNAMES)
Keep in mind that this solution is NOT particularly i18n friendly. If i18n is an issue, I would also check out the localized dates plugin and the changes that were made in Rails 2.2 to support i18n.

Try this:
<%= select_tag(:week_day, options_for_select([['Sunday', 'Sun'], ['Monday', 'Mon'], ...])) %>
See http://guides.rubyonrails.org/form_helpers.html#theselectandoptionstag

Related

Associations with Chartkick & Rails

I want to use chartkick to create a line graph of records, which belong to a planned task.
In other words: plantask has_many records
Records has two fields I'm interested in graphing. The created_at, which will be my X-axis, and data(An integer), which will be my Y-axis.
So far I've gotten pretty close. By inserting this into my view:
<%= line_chart #plantask.records.group_by_day(:created_at).sum(:data) %>
I can see that my x-axis is displaying perfectly. However, it appears that the y-axis is not loading the records :created_at field, but is loading the :created_at from within the plantask model. (All of my records are mapped to yesterday at 7:00pm) This seems strange to me. Any hints on what I've messed up? Thanks you guys.
It turns out that I was approaching this problem the wrong way. Group by day with sum combines every task into one, and adds the value. What I really needed was this:
<%= line_chart #plantask.records.group(:created_at).sum(:data) %>

Weekly scheduling in Rails

I want to allow teachers to be able to login to my Rails 3.2 app and be able to set when they are available. So instead of having two datetime fields where an actual date is stored is stored for starts_at and ends_at, I'd like for them just to say I'm available on "Mondays between 4:00pm and 5:00pm" with all three values being dropdowns.
The orignal way I approached this was having a string for day and using the time_select method in my form for my starts_at and ends_at. Unfortunately, time_select still comes with the date.
I'm just looking for the cleanest way to allow weekly scheduling. Is this possible? If it is, is there an easier way to do this? Thanks in advance for your tips.
take a look at
https://github.com/mzararagoza/rails-fullcalendar-icecube
its a small appointment app that i think that you can take allot out of it.

Ruby on Rails: How to create a class schedule calendar with hour by hour breakdown?

I'm a new ruby on rails coder who is trying to create a neatly displayed calendar of the week for students to check their timetables.
I have stored the lessons in a table with the following data:
:course_id, :state_unit_code, :day_of_week, :start_date, :end_date, :start_time, :end_time, :classroom_id, :campus_id, :lecturer_id
I wish to take the data from that table and transform it into an html table similar to that shown:
I've mocked it up using table, tr td rowspan colspan and so on.
If you can point me in the right direction I'd be most appreciable.
Follows on from a post by another user (allesklar): How would you build this daily class schedule?
I would recommend using a gem for this unless you are a strong confident rails programmer with some decent experience. It's a good project if you're just doing it to learn but not so much if you actually want it to be used as a 'production app. in the real world.
I think that https://github.com/elevation/event_calendar might meet your needs. Take a look and see.
You may also need to use a separate gui date picker at some point and their are many solutions for that such as http://code.google.com/p/calendardateselect/ though this is just about picking dates, not the full calendar display of event also. But it could be handy. You'll also see 30 (!) different date pickers here: http://www.1stwebdesigner.com/freebies/jquery-calendar-plugins/ that also include ones that let you span dates.

How do you store recurring time periods on a database? Like "from the first of january to the last day of may"?

I have to store recurring date periods, similar to the one in the title, and I have no idea if there is an optimal day to do this. The first solution I came up with was to have day and month fields for start and end dates, but this solution doesnt sound very right to me.
I am using Ruby on Rails with SQL.
To store recurring dates of arbitrary complexity, you want to look into temporal expressions.
Some gems available are: runt, TExp or icecube
I've used both runt and icecube, as well as storing recurring dates (weekly schedules) in serialized ruby objects. The gems are the most flexible, if a little hard to use when your use case is simple.
Also if you need to parse textual expersions, look at Chronic
In ruby, you could probably express this nicely (for a particular year) with a Range:
(Time.utc(2012, 1, 1)..Time.utc(2012, 5, 1).end_of_month)
But storing this in a database...can only think of doing what you said (columns for start/end day and month), and then adding wrapper methods on the model similar to this:
def current_start
Time.utc(current_year, start_month, start_day)
end
...
def current_range
(current_start..current_end)
end
I would just use two dates... afaik that maps nicely to ruby Date objects anyway, with which you can do the usual arithmetic.

Keeping dates in order when using date_select and discarding year in Rails?

My app has users who have seasonal products. When a user selects a product, we allow him to also select the product's season. We accomplish this by letting him select a start date and an end date for each product.
We're using date_select to generate two sets of drop-downs: one for the start date and one for the end date.
Including years doesn't make sense for our model. So we're using the option: discard_year => true
To explain our problem, consider that our products are apples. Vendor X carries apples every year from September to January. Years are irrelevant here, and that's why we're using discard_year => true. However, while the specific years are irrelevant, the relative point in time from the start date to the end date is relevant. This is where our problem arises.
When you use discard_year => true, Rails does set a year in the database, it just doesn't appear in the views. Rails sets all the years to 0001 in our app. Going back to our apple example, this means that the database now thinks the user has selected September 0001 to January 0001. This is a problem for us for a number of reasons.
To solve this, the logic that I need to implement is the following:
If season_start month/day is numerically before season_end month/day, then standard Rails approach is fine.
But, if season_start month/day is numerically AFTER season_end month/day, then I need to dynamically update the database field such that the year for season_end is equal to the year for season_start + 1. For example, if a user selects October 15 to January 15, what he intends is October 15, 0001 to January 15, 0002. I need the app to reflect this.
My best guess is that I would create a custom method that runs as an after_save or after_update in my products model. But I'm not really sure how to do this.
Ideas? Anybody ever had this issue? Thanks!
Add a before_save callback in your Product model.
class Product < ActiveRecord::Base
before_save :fix_end_date
def fix_end_date
self.end_date +=1.year if start_date > end_date
end
end

Resources