I want to create a month drop down list so that the previous months of the current year do not appear.
Right now my code is :
<%= f.date_select :card_expires_on, :discard_day => true, :start_year => Date.today.year, :end_year => (Date.today.year+10), :add_month_numbers => true %>
it shows me all the months now, but I want to remove or hide the past months..Is there a way?
Thanks in advance! :-)
I haven't tested this but I think it should work.
<%= select nil, "name_of_your_model[card_expires_on(1i)]", (Date.today.year..Date.today.year+10).collect { |y| y }, options = {}, id: "card_expires_on_1i" %>
<%= select nil, "name_of_your_model[card_expires_on(2i)]", (Date.today..Date.today.end_of_year).collect { |d| ["#{d.strftime('%m')} - #{d.strftime('%B')}", d.strftime('%m')] }.uniq, options = {}, id: "card_expires_on_2i" %>
<%= text_field_tag "name_of_your_model[card_expires_on(3i)]", nil, type: 'hidden', value: Date.today.day, id: "card_expires_on_3i" %>
Basically when you put date_select in your form it creates three selects. Rails then uses the "(1i)", "(2i)", and "(3i)" to bring the year, month, and day, respectively, back together. Make sure you change "name_of_your_model" to the name of the model used for the form and the value of the text_field to what ever default day you want.
Update
I just tested this and it did work!
Seems to me like you should be able to add this to a helper. I'd shy away from javascript
adding your own method for remaining_months() or some such thing would be much simpler to use.
Related
I have this line in my rails app:
<%= select_tag :questionnaire_id,
options_for_select(#questionnaires_types, #questionnaires_ids),
:multiple => true, :size => 7 %>
which works fine.
but when I try to use the multiple values that were selected I get this:
questionnaire_id"=>["1687,1688,1689,1690,1691,1724"]
instead of this:
questionnaire_id"=>["1687", "1688", "1689" ,"1690", "1691", "1724"]
i.e. I get 1 item instead of 6 items.
any suggestions?
According to rails code: https://github.com/rails/rails/blob/41231ef6c6c6a6e546b69add28f04aafb9e0e952/actionview/lib/action_view/helpers/form_tag_helper.rb#L134
The name must end with [] to be make sure you receive an array.
def select_tag(name, option_tags = nil, options = {})
option_tags ||= ""
html_name = (options[:multiple] == true && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name
if options.delete(:include_blank)
option_tags = content_tag(:option, '', :value => '').safe_concat(option_tags)
end
if prompt = options.delete(:prompt)
option_tags = content_tag(:option, prompt, :value => '').safe_concat(option_tags)
end
content_tag :select, option_tags, { "name" => html_name, "id" => sanitize_to_id(name) }.update(options.stringify_keys)
end
So just change it to questionnaire_ids[]
Hope that helps.
I think a collection_select would look nice but I cannot help with that since you did not post anything about the model. Maybe try this so that it knows it is a collection:
<%= select_tag "questionnaire_ids[]", options_for_select(#questionnaires_types, #questionnaires_ids), :multiple => true, :size => 7 %>
Or you could just parse the string you currently receive using #split.
Otherwise post a bit more code about the associations between Questionnaire and what ever this model is.
Well, just in case that someone will come to this issue, I found the problem.
It seems to be a bug in rails.
I was using remote_form_for, and that gave me the strange behaviour. I tried to change the form to form_for instead, and I got an array with 6 items.
Rails, Rails, when will you be like .Net? :-(
How do you use Rails date_select form helper and generate a seperate ng-model for month and for year?
// Slim
= f.date_select :from, {
order: [:month, :year],
start_year: Time.now.year,
end_year: 1990,
include_blank: true },
{ :'ng-required' => 'isRequired'
}
The date_select makes it easy to set differenct prompts per each field. Is there any way to do this with your custom attributes?
date_select("article", "written_on", prompt: { day: 'Select day', month: 'Select month', year: 'Select year' })
The problem is that date_select creates 3 select_tag fields(one for month, year, and day). You can add the ng-model tag within html_options for date_select, but the problem is that all 3 select fields will have an identical ng-model tag with the same name. Angular doesn't like having multiple ng-model objects with the same name, and I don't know how to get around this. The solution I used was just to recreate what date_select does and create 3 select_tag fields. It's definitely not as elegant as the date_select field but it does work. I'd love to hear if anyone has a better solution!
In my app, I let people select their start year for a job. This works fine when creating a new job, but on the edit form I want the year the user selected during create to be automatically population.
My code:
<%= select_year Date.today, start_year: Time.now.year, end_year: Time.now.year - 95, field_name: :start, prefix: :job %>
Currently, it just defaults to 2013 everytime. I know I am passing in the instance variable correctly because the other fields are populated automatically, just not this year dropdown.
How do I get this to work?
This answer was too long to put in the comments, so I'm putting it here as an answer, but let me know if it answers your question or not.
You could manually set the start_year and end_year variables to, for example, 1950 and Time.now.year. Then, set the first argument in select_year helper to the instance variable to make it the default selection between the wide range instead of Date.today.
For example:
<%= select_year #instance_variable, start_year: 1950, end_year: Time.now.year, field_name: :start, prefix: :job %>
More examples are available in the Rails Docs, too:
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_year
I'm trying to use Rails date helpers with a form. However, I only want to get the year. I got the form code below from simple_form gem https://github.com/plataformatec/simple_form. The discard_day and discard_month prevent the month and day select box from showing. However, when I submit the form, I'm getting this multiparameter error
2 error(s) on assignment of multiparameter attributes
"start(1i)"=>"2013",
"start(2i)"=>"4",
"start(3i)"=>"23",
"end(1i)"=>"2011",
"end(2i)"=>"4",
"end(3i)"=>"23",
It appears Rails is just substituting the current day and month since none was entered in the form.
I'm wondering if there's a way to prevent Rails from including the current day and month, or am I supposed to deal with it in the controller. Right now, the create action looks like this
def create
#employment = current_user.employments.build(params[:employment])
if #employment.save
....
If I can't stop Rails from including the current day and month, then what would I do in the controller?
<%= f.input :start, as: :date, start_year: Date.today.year - 90,
end_year: Date.today.year, discard_day: true, discard_month: true %>
<%= f.input :end, as: :date, end_year: Date.today.year - 90,
end_year: Date.today.year, discard_day: true, discard_month: true %>
What is your data type for the start and end columns? If you are just storing a year, you can set them to an integer type instead of a date, and use <%= f.input :start, collection: (Date.today.year - 90)..(Date.today.year) %>.
I have created a custom date_Select field using 3 separate select fields:
<%= f.select :day, options_for_select(User::DAYS), :include_blank => "Day:" %>
<%= f.select :month, options_for_select(User::MONTHS), :include_blank => "Month:" %>
<%= f.select :year, options_for_select(User::YEAR_RANGE), :include_blank =>"Year:" %>
In my User.rb (Model) I have this validation rule and also using validates_timelessness gem:
MONTHS = ["January", 1], ["February", 2]..etc
DAYS = ["01", 1], ["02", 2], ["03", 3]..etc
START_YEAR = Time.now.year - 111
END_YEAR = Time.now.year
YEAR_RANGE = START_YEAR..END_YEAR
validates :birthday, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date, :on_or_before_message => "Select a valid birthday"}
I have created some tests which work perfectly fine with the date_select that comes with rails but that date_select is buggy which is why I opted for a custom one. My only issue now is I wish to get day, month and year to work with my :birthday symbol. How do I combine all 3 so that my :birthday symbol can use the select data? If that makes sense...
The date_select would have been perfect but it lets users submit a form without the yea being filled out and if a users chooses 1 for a day and clicks submit it will automatically select january. I haven't found a way round that.
So I'm using 3 separate select fields which I want to combine and make work with :birthday just like date_select did.
Help is appreciated.
i would recommend using a date-select pop-up.
There are several gems available. I have used the one detailed in http://www.rubyinside.com/calendar-date-select-a-lightweight-prototype-based-datetime-picker-for-rails-developers-573.html with success.
Once you have a real date field you'll be in a better position to perform date type validations including ranges and presence that you can be confident in worrectly with that type of data.
The select_date isn't buggy, you need to perform the necessary data validation on your end so an empty year isn't allowed. Validation is designed to let you specify what data is allowed into your app. Only you can be the gatekeeper of what is considered 'valid'.