use Rails date_select with Angular - ruby-on-rails

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!

Related

In Rails, Convert string from date_select to Date

I have a string like this:
{2=>1, 3=>1, 1=>2008}
It was generated from a select field like this:
<%= user_record.date_select :response, {
order: [:month, :day, :year],
prompt: {
day: 'Select day',
month: 'Select month',
year: 'Select year'
},
start_year: Date.today.year - 12,
end_year: Date.today.year - 110
} %>
And stored as a string in the database. (I can't change this to a date type, because it's user generated content).
This causes 2 problems:
I want to show a humanized version of the date on the Show view. Right now it looks like {2=>1, 3=>1, 1=>2008}
I want to show a date select input on the Edit View form field. Right now it errors with
undefined method `year' for "{2=>1, 3=>1, 1=>2008}":String
I have tried splitting the string, and stripping characters, but is there a better solution in Ruby?
You could add a customer setter and getter method to your model to translate between the incoming hash from the view and the string type database column.
Something like this might work:
def response
Date.parse(super)
end
def response=(value)
value = Date.new(value[1], value[2], value[3]) if value.is_a?(Hash)
super(value.to_s)
end

Rails - how to set default on edit form for a year select form

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

How to stop rails from including current day and month in date helper

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) %>.

Disable Previous Months in date_select Ruby on Rails

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.

Ruby on rails 3 select box values combined ready for validation, is this possible?

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'.

Resources