I have a model articles with created_at
In my index view I want the user to be able to select all articles created in "October 2010, November 2010, December 2010, etc." with only one select dropdown.
Has anyone done this before?
Rails 3 (MySQL)
class Article < ActiveRecord::Base
...code here...
def self.article_months
group_by_clause = "to_char(created_at,'<your date format>')"
Article.group(group_by_clause).select("#{group_by_clause} as month, count(*) as count").order(created_at desc).all
end
...code here...
end
In your view, use select_tag or collection_select or something and render the collection above.
In my controller I did:
#startdates = ActiveRecord::Base.connection.select_rows("SELECT DISTINCT MONTH(start_date), YEAR(start_date) FROM schedules WHERE event_id IN (#{#events.map{|n| n.id}.join(',')}) ORDER BY start_date ASC;")
and in my view:
<%= select(:event, :startdate, #startdates.map{|s| ["#{t("date.month_names")[s[0].to_i]} #{s[1]}", "#{s[1]}-#{s[0]}"]}, {:selected => #startdate.to_s, :include_blank => "All"}) %>
Related
My User model looks like this. User has many attached Resume.
class User < ApplicationRecord
has_many_attached :resumes, dependent: :destroy
end
Now in different form, I want to show list of resumes upload by user in descending order. I manage to do that by using following code.
<%= f.select :resume, options_for_select(#user.resumes.includes(:blob).references(:blob).pluck(:filename, :created_at).reverse.map{ |e| e.join(' - ') }), prompt: '-- Select --', class: 'form-control' %>
So now in my select drop down I am able to see all the uploaded resume in descending order. My dropdown looks like this file_name.pdf - 2021-11-30 03-59-59 UTC. I don't want to show time in dropdown. I just want to format it and show date with filename, something like this john_resume.pdf 11/11/2021.
So my question is how can I format the date when using pluck ?
<% options = #user.resumes.includes(:blob).references(:blob).pluck(:filename, :created_at).reverse.map { |e| filename = e.first; created_at = e.last; [filename, created_at.strftime('%d/%m/%Y')].join(' - ') }%>
<%= f.select :resume, options_for_select(options), prompt: '-- Select --', class: 'form-control' %>
I have a form which allows user to select date, in the view i have it so the user only selects the year, but when the date is saved, it saves todays day and month along with it? all I want it to display is the year, which the user selects, this is my code:
<%= f.date_select :finishdate, :order => [:year] %><br>
If you wish to show only the year value in the form and store it in the database without the month and day, then you can have an integer field and only show the year value as follows:
<%= f.select :finishdate,Date.today.year-10 .. Date.today.year+10 %>
you can try
<%= f.select_year :finishdate, :order => [:year] %><br>
What is the end result that you'd like to work with? If it is only the year, you'll probably not want to use the date_select as that, I believe, will return a DateTime instance and not just the year.
If you do need just the year, you can look into other helpers like select_year.
I have a form with a date attribute.
Right now I'm using f.date_select :begin_date, discard_day: true but that displays month and year in a each own's select menu respectively.
Is it possible to have both month and year displayed in the same select menu, like below?
2013 January
2013 February
2013 March
2013 April
etc.
It's 7 years later, but this is still is still coming up when I Googled for this exact problem.
You can make it work with a standard ActionView::Helpers::FormBuilder#select and feed it the set of month+years by hand
<% the_months = (Date.new(2020,1,1)..Date.new(2020,12,31)).select { |d| d.day == 1 } %>
<%= f.select :start_date, the_months.map { |d| [d.strftime('%b %Y'), d] } %>
You can have a look to rails API documentation AND date select method for more details.
Your code will be something like this:
<%= f.date_select :date, { :discard_day => true} %>
I had a similar problem where I wanted just one field with a datepicker where the user could only select a month and a year. Today I found out about the month_field FormHelper which solved the problem for me.
I know it's not an answer to the problem the original poster asked about but hopefully it can help others googling for the same problem.
<%= f.month_field :date %>
I currently have one text field for a date entry, I am trying to split the year, month and day up into three individual entries, seperated by '/'. The original text entry looks like:
<%= f.text_field :date, :placeholder => 'YYYY/MM/DD' %>
I would like to split this into three text_fields, and append them together, and put it into the date entry in the database.
How can I do this?
You often use textfields for dates because you then only needs one field. When you are having three different fields should you consider using select instead. Rails have a the date helper method date_select, so it would be somethink like this:
<%= f.date_select :date %>
This creates one select for years, one for months and one for days.
You can read more on http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select
Add three virtual attributes to the model:
attr_accessor :form_month, :form_day, :form_year
Then put the following in the controller, not the model:
def create
# ...
form_date = [ params[:form][:form_month], \
params[:form][:form_day] ,
params[:form][:form_year] ].join("/")
#my_model.date = Date.parse(form_date)
# ... save and return ...
end
It would be a good idea to manually check each form parameter for validity. Date#parse may spit out the incorrect date if fed an incomplete date string.
Date.parse "2005/11/4"
# => Fri, 04 Nov 2005
Date.parse "/11/4"
# => Mon, 04 Nov 2013
Try to use https://github.com/plataformatec/simple_form
<%= f.input :deadline, :start_year => Date.today.year, :end_year => Date.today.year + 1, :order => [:day, :month, :year] %>
Do you have many options.
I have a form where the user will select a date range, and then a report will be presented based on that date range.
The relevant bit of my Rails form looks like this...
<div class="field">
<b>Start Date</b><br />
<%= date_select :startDate, options = {:order => [:day, :month, :year]} %>
</div>
<div class="field">
<b>End Date</b><br />
<%= date_select :endDate, options = {:order => [:day, :month, :year]} %>
</div>
However, I'm having trouble figuring out how to access the resulting dates in the controller. I've put the following diagnostics in the controller,
puts params[:startDate]
puts params[:endDate]
puts Date.today
...and this shows,
orderdaymonthyear(1i)2011orderdaymonthyear(2i)3orderdaymonthyear(3i)21
orderdaymonthyear(1i)2011orderdaymonthyear(2i)10orderdaymonthyear(3i)21
2011-10-21
My search, is defined at follows,
SalesActivity.find(:all, :conditions => {:created_at => params[:startDate]..params[:endDate]})
...and this gives the exception, 'bad value for range'.
Pointers to how to pass and use a Date range would be appreciated.
Thanks to the helpful suggested answers, I learned that the area I needed to dig into was multiparameter attributes. However, the answer referenced by Julik seems more complex than what I need.
However, I did find this,
http://snippets.dzone.com/posts/show/4630
Which gave me what I was looking for so, my [now working] code looks like this,
#start_date = Date.civil(params[:range][:"startDate(1i)"].to_i,
params[:range][:"startDate(2i)"].to_i,
params[:range][:"startDate(3i)"].to_i)
#end_date = Date.civil(params[:range][:"endDate(1i)"].to_i,
params[:range][:"endDate(2i)"].to_i,
params[:range][:"endDate(3i)"].to_i)
Thanks again for the help.
This answer explains what these parameters are and how they are structured. AFAIK for now Rails does not offer an easy way to add multiparameter assignment support to an arbitrary object (which is what you would want in this case).
Try SalesActivity.all.where("created_at > ? AND created_at < ?", params[:startDate], params[:endDate])
Technically this is not an answer to your specific question, but it is a solution to your problem.
Have you considered using a date select javascript calendar helper? As such in your user interface your user has to select from 6 drop down lists. It may be simpler for them to select 2 from 2 calendar instances. You will then get the value for start date and end date as strings.