Refer to
http://guides.rubyonrails.org/form_helpers.html#using-date-and-time-form-helpersSection
4.2

This is Rails. You should expect that it helps you with banal stuff like
this, And it does!

On 8 November 2012 11:51, Steven Ringo <[email protected]> wrote:

>  Rails still sends them through as separate fields, in this case for month
> and year, and they need to be recombined manually.
>
>  On Thursday, 8 November 2012 at 10:29 AM, Mark Ratjens wrote:
>
> Another way to do this is store a date as one field on the model. It's
> quite easy to add methods to the model to present month and year values so
> that you effectively have the same model interface that you need. I'd be
> tempted to do this, since it relies less on less familiar AR features.
>
> In the form, you do can something like
>
> f.date_select :credit_card_date, "written_on", :discard_day => true
>
> so that the day component of the day is neither shown on the form nor sent
> through the params.
>
>
> On 8 November 2012 09:50, Ivan Vanderbyl <[email protected]> wrote:
>
> Ah yes, I went through this same problem a few weeks ago with a CC form.
> Ended up doing the same thing.
>
>
> On 08/11/2012, at 9:46 AM, Steven Ringo <[email protected]> wrote:
>
>  Hi,
>
> @Ivan, thanks :-)
>
> Since this is a credit card field, we are going to just have separate
> explicit fields for month and year, as that's what the acquirer wants
> anyway.
>
> I was worried about how to handle validations on the combined date, but we
> don't need to validate at that level. Rather we validate for all the credit
> card fields together.
>
> Perhaps it might be a good idea to see multi-parameter attributes factored
> out of AR and into ActiveModel?
>
> Thanks again.
>
> Steve
>
> On Thursday, 8 November 2012 at 9:39 AM, Ivan Vanderbyl wrote:
>
> Hi Steve,
>
> This is handled by ActiveRecord usually
> in ActiveRecord::AttributeAssignment#assign_multiparameter_attributes (
> https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_assignment.rb#L62-66
> )
>
> You could experiment with either mixing in that module or pulling out the
> #assign_multiparameter_attributes methods into your own module and using it
> with ActiveModel.
>
> AFAIK there is no equivalent for ActiveModel.
>
> — Ivan
>
> On 08/11/2012, at 8:55 AM, Steven Ringo <[email protected]> wrote:
>
> Hi all,
>
> Anyone have any idea on the best way to parse a date from a rails form and
> coerce it into a date field in an active model object.
>
> Rails send this in the params hash:
>
>  "credit_card_expiry(3i)"=>"1",
>  "credit_card_expiry(2i)"=>"11",
>  "credit_card_expiry(1i)"=>"2012"
>
>
> However the model sees these as being three separate attributes, and of
> course the the active model object has no way of knowing this.
>
> I have seen techniques that manually mangle the hash like so (or
> introspective variants), but this feels very hacky.
>
> params[:subscription][:credit_card_expiry] = 
> Date.new(params[:subscription][:"credit_card_expiry(1i)"].to_i,               
>                                              
> params[:subscription][:"credit_card_expiry(2i)"].to_i, 
> params[:subscription][:"credit_card_expiry(3i)"].to_i)
>
> params[:subscription].delete(:"credit_card_expiry(1i)")
> params[:subscription].delete(:"credit_card_expiry(2i)")
> params[:subscription].delete(:"credit_card_expiry(3i)")
>
>
> Thanks,
>
> Steve
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rails-oceania/-/5N1Kll4oUZ4J.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rails-oceania?hl=en.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rails-oceania?hl=en.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rails-oceania?hl=en.
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rails-oceania?hl=en.
>
>
>
>
> --
>
>
> *Mark Ratjens*
> *Co-founder, Habanero Software*
> *
> *
> Sydney, Australia
> [email protected]
> @MarkRatjens <[email protected]>
> www.habanerohq.com <http://habanerohq.com>
> +61 414 159 357
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rails-oceania?hl=en.
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rails-oceania?hl=en.
>



-- 


*Mark Ratjens*
*Co-founder, Habanero Software*
*
*
Sydney, Australia
[email protected]
@MarkRatjens <[email protected]>
www.habanerohq.com <http://habanerohq.com>
+61 414 159 357

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to