Forum: Ruby on Rails number_to_currency() in the view?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-05-02 14:44
On p. 93, AWDWR(3rd) it says,

--------
...we could do this:

<span><%= sprintf("$0.02f", product.price) %</span>

This would work, but it embeds knowledge of currency formatting into the
view.  Should we want to internationalize the application later, this
would be a maintenance problem.
---------


<Goody.  Let's move the formatting out of the view and into a more
centralized location.  The Product model?  Hmm...maybe, but not all
calls to the Product model will want the number converted to a formatted
string.  The ProductsController then?  Yes, yes.  That must be it!>

<turn the page>


--------
Instead, let's use a helper method to format the price as a currency.
...
...
<span><%= number_to_currency(product.price) %</span>
--------

Wt??  How does that solve any maintenance issues.  If I later want to
change the view to display a different format, what's the difference
between having to change the parameters of number_to_currency()
everywhere it appears vs. changing the format of sprintf()?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-02 16:28
(Received via mailing list)
On May 2, 1:44 pm, 7stud -- <rails-mailing-l...@andreas-s.net> wrote:
> ...
> <span><%= number_to_currency(product.price) %</span>
> --------
>
> Wt??  How does that solve any maintenance issues.  If I later want to
> change the view to display a different format, what's the difference
> between having to change the parameters of number_to_currency()
> everywhere it appears vs. changing the format of sprintf()?

Well rather than having format strings spread all over the place, you
centralized the concept of formatting a number in one place, the
number_to_currency method. If you wanted to change currency formatting
you'd just have to change that one helper.

Fred
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-05-03 01:15
Frederick Cheung wrote:
> On May 2, 1:44�pm, 7stud -- <rails-mailing-l...@andreas-s.net> wrote:
>> ...
>> <span><%= number_to_currency(product.price) %</span>
>> --------
>>
>> Wt?? �How does that solve any maintenance issues. �If I later want to
>> change the view to display a different format, what's the difference
>> between having to change the parameters of number_to_currency()
>> everywhere it appears vs. changing the format of sprintf()?
>
> Well rather than having format strings spread all over the place, you
> centralized the concept of formatting a number in one place, the
> number_to_currency method. If you wanted to change currency formatting
> you'd just have to change that one helper.
>

How do you override a helper method?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-03 11:41
(Received via mailing list)
On May 3, 12:15 am, 7stud -- <rails-mailing-l...@andreas-s.net> wrote:
> How do you override a helper method?
>
Same as any other method ? (although I suppose helper :all might throw
a slight spanner in the works)

Fred
This topic is locked and can not be replied to.