Rails date select assigns month a value when blank - ruby-on-rails-3

I am using Rails date_select as follows:
<%= date_select :user, :birthday, {:start_year => Time.now.year, :end_year => 1910,:order => [:day,:month,:year], :prompt => { :day => 'day', :month => 'month', :year => 'year' }}, { :class => "default" } %>
When a user forgets to enter a value for the day or month and submit the form when the information is put into the model an error is generated. However the day and month fields also get assigned to 1. This results in the form showing 1 for the day field and January for the month field.
How can I stop this from happening?

Related

display subseconds in time_select in Rails form

I have a form to record sports times : I need minutes, seconds and hundredths of second, i.e. 1:31.43 --- I don't need the hour.
In my form I use :
<%= f.time_select :perf_time, {:discard_hour => true, :default => {:minute => '00', :second => '00'}, :include_seconds => true} %>
This displays 2 select pull-downs, one for minutes and one for seconds.
I have added a separate field for hundredths of second (type Integer):
<%= f.number_field :perf_time_cents, :in => 0..999 %>
Now I'd like to use the method .change()in my helper to change add/change the microseconds to perf_time. Here's my code, but it does not do anything.
before_save :set_perf_time
def set_perf_time
self.perf_time.change(usec: (self.perf_time_cents * 10))
end
There is no option for milliseconds in time_select because a combobox with 1000 possible values would not be user friendly.
You could use instead a separate text field for the number of milliseconds, with the HTML5 type number:
<%= f.number_field :perf_time_millis, :in => 0..999 %>
In the controller, use both the values in the selects and the text input field to get the full time in millis:
time_in_millis = params[:perf_time_millis].to_i + 1000 * (params["perf_time(5i)"].to_i * 60 + params["perf_time(6i)"].to_i))

bootstrap-datepicker-rails, show calendar only on clicking calander-icon(not on text field)

I,m using bootstrap-datepicker-rails gem for date picker and want to shoe calender only on icon click(not on clicking text filed).
Write Now I'm using in my form:
= form_for (ServiceExp.new), :remote => true do |s|
%ul
%li
= s.label :position
= s.text_field :position
%li
= s.label :start_date
%p
From
%li
.input-append.date.datepicker
= s.text_field :start_date, :class =>"input-mini"
%span.add-on
.calander-icon
To
.input-append.date.datepicker
= s.text_field :end_date, :class => "input-mini"
%span.add-on
.calander-icon
%li
= s.label :description
= s.text_area :description
= s.submit 'Save',:class => "btn btn-primary"
And in javascript:
$('.datepicker').datepicker();
Here, Calendar shows on both text field and icon because class datepicker on top of both. If use this class only on icon than it show calender only on icon click but not adding value on text field. How can I achieve that one. please give any suggestion !!
Add :"data-date-format" => 'dd-mm-yyyy', :'data-date' => '12/2/2013' # default date
To div container containing input box and span.
Like
%li
.input-append.date.datepicker{:"data-date-format" => 'dd-mm-yyyy', :'data-date' => '12/2/2013'} # default date
= s.text_field :start_date, :class =>"input-mini"
%span.add-on
.calander-icon

How to list all times from 20:00 untill 24:00 in a collection and only list the times > Time.now

Im trying to populate a collection with Times like 20.00, 20.10, 20.20 ... 24:00. So in intervals of 10.minutes. But how to do this smartly and take into account the Time.now?
Only times that are > Time.now should be listed.
So if its 20.30 It should not show 20.10, 20.20,20.30
Example code
= f.input :order, :collection => ["20:00","20:10","20:20"... etc ["24:00"],
:default => 2,
:label => "orders,
:hint => "Select the time you want this order to be processed"
Some of the things Ive tried so far:
:collection => [(Time.now + 10.minutes).strftime("%I:%M%p").to_s]
and
#hours=(Time.now.minus_with_coercion(Time.now.midnight)/3600/2)
Any thoughts how to cleanly code this ? Thank you
Not sure to understand your problem but this could help :
Time.parse('20:00').to_datetime.step(Time.parse('23:59'), 10.minutes).to_a.map {|date| date.strftime("%I:%M%p")}
=> ["08:00PM", "08:10PM", "08:20PM", "08:30PM", "08:40PM", "08:50PM", "09:00PM", "09:10PM", "09:20PM", "09:30PM", "09:40PM", "09:50PM", "10:00PM", "10:10PM", "10:20PM", "10:30PM", "10:40PM", "10:50PM", "11:00PM", "11:10PM", "11:20PM", "11:30PM", "11:40PM", "11:50PM"]
After that, you could call the delete_if method to remove unwanted time.
Something like that :
Time.parse('20:00').to_datetime.step(Time.parse('23:59'), 10.minutes).to_a.delete_if {|date| date < DateTime.now.to_time}.map {|date| date.strftime("%I:%M%p")}

How to get number of items in a grouped row in Ruby on Rails?

I am finishing up my first RoR project, and am working on a leaderboard system that shows the number of points users have accrued for correctly answering quiz questions.
I am getting all of the users that have answered at least one question correct, grouping them by user_id, and displaying them in descending order by most correct using this:
#users = Point.find(:all,
:group => 'user_id',
:order => 'correct DESC', :conditions => { :correct => "yes"})
In my view, I am using this to iterate through the results:
<% #users.each_with_index do |user, index| %>
However, I am not able to get the number of correct answers per user. I tried:
user.count
but that doesn't work. How do I get the number of items per group?
You're on the right track. Seems like you would be better off using the all command with the count condition within it as opposed to the count command. Something like this:
Point.all(:select => 'user_id, count(id) as point_count', :group => :user_id, :conditions => { :correct => 'yes' }, :order => 'point_count desc', :limit => 10)
This will return 10 limited Point objects with a user_id attribute (so you can still access the user relationship), and a point_count attribute with the number of correct points said user has obtained.
Note: you could change the limit to be however many users you wanted to display in your leaderboard. This example would return 10.
It might make more sense to have your code look like this:
#points = Point.all(:select => 'user_id, count(id) as point_count', :group => :user_id, :conditions => { :correct => 'yes' }, :order => 'point_count desc', :limit => 10)
And as I said in a comment below, you could iterate through them by doing something like this (this would assume that your User model has a name attribute):
<table>
<% #points.each do |point| %>
<tr>
<td><%= point.user.name %></td>
<td><%= point.point_count %></td>
</tr>
<% end %>
</table>
I think the problem may be that you think you're getting an Array back, but you actually get a Hash back.
Try doing:
p #users
(which is equivalent to puts #users.inspect). You'll probably see it's more so something like:
{ "1" => [UserObject, UserObject], "2" => `[UserObject] }
You can even do p #users.class and you'll see it's not an array.
When you loop with a .each_with_index on a Hash, you need to do:
#users.each_with_index do |(key, value), index|
Then you can do #users[key].count or value.count.
Figured out how to get the correct count:
#users = Point.count(:group => :user_id, :conditions => { :correct => "yes"})
The most simple way should be:
#user.points.where(:correct => "yes").count
Though this will only work if have defined your associations in the user and point model like
class User < ActiveRecord::Base
has_many :points
class Point < ActiveRecord::Base
belongs_to :user
(personally I would have used a bool flag (smallint) instead of string for the "correct" column.

Issue with ":prompt" in select_year with start/end_year

Using this answer as a guide, I set out to create a select_year that started with today's year and ends 100 years ago. But I wanted to add a :prompt so that the drop down starts with "Year" instead of the current year. So I used the following:
<%= select_year(Date.today, {:prompt => "Year", :start_year => DateTime.now.year, :end_year => DateTime.now.year - 115}, {:field_name => 'Year', :id => 'Date.year'}) %>
This renders a drop down for year, but the prompt shows current year instead of "Year". If I click the drop down though, it shows "Year" as the first option with current year selected. How can I fix this? What did I do wrong?
UPDATE: Here is the HTML output of the above code:
<select field_name="Year" id="Date.year" name="date[year]">
<option value="">Year</option>
<option selected="selected" value="2011">2011</option>
How can I make it so 2011 isn't automatically "selected"?
solution was simple
<%= select_year(0, {:prompt => "Year",
:start_year => DateTime.now.year,
:end_year => DateTime.now.year - 115},
{:field_name => 'Year', :id => 'Date.year'}) %>
I suppopse if default value is out of range you have first of the list selected which is prompt