# Change date and numbers format, before\_validation()

**URL:** <https://www.ruby-forum.com/t/change-date-and-numbers-format-before-validation/161958>\
**Category:** Rails\
**Created:** [March 4, 2009, 7:56am UTC](https://www.ruby-forum.com/t/change-date-and-numbers-format-before-validation/161958 "2009-03-04T07:56:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [March 4, 2009, 7:56am UTC](https://www.ruby-forum.com/t/change-date-and-numbers-format-before-validation/161958/1 "2009-03-04T07:56:41Z")

</div>

Hello,

I need to change/adapt of the date and float numbers the user enters:

dd/mm/yyyy I have to change into yyyy-mm-dd

and numbers, replace the , with the .

1245,85 =\> 1245.85

I thought the best place was using the before\_validation() but here the  
objects come with standard Rails formats, and the date is wrong, and the  
numbers are also wrong.

Now I’m doing all the stuff in the Controller:

def update

@transaction=Transaction.find(params[:id])

if @transaction.update\_attributes(params[:transaction])

@transaction.data=convert\_european\_date(@transaction.data.to\_s)  
@transaction.amount = params[:transaction][:amount].to\_s.gsub(’,’,  
‘.’).to\_f

end

…

But this is a pain If I have to duplicate code for new and update  
controllers method.

So I tried to move the code into the model before\_validation but there  
the self.data and self.amount are wrong, as Rails adapt them to what  
they think it’s the standard way …

In the controller I can get the fresh value from params, but in the  
model it doesn’t exist …

what’s the best solution to my problem ?

thanks!

r.

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [March 4, 2009, 9:10am UTC](https://www.ruby-forum.com/t/change-date-and-numbers-format-before-validation/161958/2 "2009-03-04T09:10:38Z")

</div>

since this problem is closely linked to your user’s entries, you gotta  
solve it in your view or controller. to not repeat yourself too much  
you could add a helper-method for that piece of code you are using to  
set your records straight.

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [March 4, 2009, 9:15am UTC](https://www.ruby-forum.com/t/change-date-and-numbers-format-before-validation/161958/3 "2009-03-04T09:15:28Z")

</div>

MaD wrote:

> since this problem is closely linked to your user’s entries, you gotta  
> solve it in your view or controller. to not repeat yourself too much  
> you could add a helper-method for that piece of code you are using to  
> set your records straight.

yes and no, this problem is the same for lots of users that doesn’t  
follow the US format number and dates …

I’m using the helpers for displaying the records, but I can’t use them  
in the controller, so I created two methods in the Application  
Controller, at least it’s working but not the way Rails is proposing …

thanks!

r.

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [March 4, 2009, 9:42am UTC](https://www.ruby-forum.com/t/change-date-and-numbers-format-before-validation/161958/4 "2009-03-04T09:42:40Z")

</div>

MaD wrote:

> well, helpers are really meant for helping with displaying your data.  
> thats right. i actually didn’t mean to write a helper-method, but a  
> method (in your controller) to help you with setting your records. my  
> fault (got lost in translating my thoughts).  
> i think your aproach of putting it into application-controller is a  
> good one (as long as you really need that method in more than one  
> controller).

yes, you’re right: helpers are for help no matter where they help …

🙂

r.

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [March 4, 2009, 9:40am UTC](https://www.ruby-forum.com/t/change-date-and-numbers-format-before-validation/161958/5 "2009-03-04T09:40:05Z")

</div>

well, helpers are really meant for helping with displaying your data.  
thats right. i actually didn’t mean to write a helper-method, but a  
method (in your controller) to help you with setting your records. my  
fault (got lost in translating my thoughts).  
i think your aproach of putting it into application-controller is a  
good one (as long as you really need that method in more than one  
controller).

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [March 4, 2009, 11:43am UTC](https://www.ruby-forum.com/t/change-date-and-numbers-format-before-validation/161958/6 "2009-03-04T11:43:33Z")

</div>

On Wed, Mar 4, 2009 at 9:39 AM, MaD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> well, helpers are really meant for helping with displaying your data.  
> thats right. i actually didn’t mean to write a helper-method, but a  
> method (in your controller) to help you with setting your records. my  
> fault (got lost in translating my thoughts).  
> i think your aproach of putting it into application-controller is a  
> good one (as long as you really need that method in more than one  
> controller).

One question I have for some time now is:

What is the idiomatic place in the directory tree for positioning such  
“controller\_helpers” ?

…/app/helpers/ is really for view helpers.

Of course, they could go in

…/app/controllers/application.rb

but then, that is inherited by _all_ controllers.

We now put some “controller-helpers” in modules under /lib/  
and then include those modules selectively in controllers.

Or should we use sub classes for everything, but I read that is not  
the “Rails-way” either.

Would it make sense to have default locations for “controller\_helpers”.

Maybe something along the lines of:

/app/view\_helpers  
/app/controller\_helpers  
/app/helpers → /app/view\_helpers

The concept of

helper :all (that we actually use now in  
…/app/controllers/application.rb)

also seems broken as this is unclean mixing of view and controller  
helpers.

How should I do this in a clean fashion ?

Thanks,

Peter

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [March 4, 2009, 2:45pm UTC](https://www.ruby-forum.com/t/change-date-and-numbers-format-before-validation/161958/7 "2009-03-04T14:45:44Z")

</div>

well, you could always create a helper with an arbitrary name and  
include them in your controller. that way helpers are all huddled  
together in one folder, but that special controller\_helper gets only  
included in controller that depend on it.
