Topic: Currencies

Hi,

I am working through the 2nd edition of Agile Web Development, and I have gotten to the part where we can use a built in helper to format the price.  Well I live in the Uk and I want to use my own currency symbol and not the dollar sign.

I believe that I need to install Rails into Vendor folder?

Is there another way of doing this?

Re: Currencies

The method used is called number_to_currency. This takes parameters as you can see in the docs. Like this:

number_to_currency(..., :unit => "£")

There's no need to install Rails in the vendor directory for this.

Railscasts - Free Ruby on Rails Screencasts

Re: Currencies

So I can just alter this code:

<span class="price"><%= number_to_currency(product.price) %></span>

Re: Currencies

Yep, to this:

<span class="price"><%= number_to_currency(product.price, :unit => "&pound;") %></span>

Railscasts - Free Ruby on Rails Screencasts

Re: Currencies

so simple?

Thanks

Out of interest though, is there anyway to modify the helpers, or is that like genetically modifying the make up of Rails?

Re: Currencies

Each method allows you to specify parameters like this. However, if this is not enough control you can override the method or create a new one that does exactly what you want.

Does this answer your question? If not, can you provide an example of what you are referring to?

Railscasts - Free Ruby on Rails Screencasts

Re: Currencies

Well the book says that you can install Rails into Vendor folder, but mine hasn't, not sure if I should though.

Re: Currencies

Well, you can, but I don't think you need to. What version of Rails do you have installed? If you want your project to work under a different version then you'll need to "freeze" Rails into the vendor directory.

Railscasts - Free Ruby on Rails Screencasts

Re: Currencies

Ok, so by freezing the gems it actually freezes Rails into Vender?  cos at the moment I only have a plugins folder in there.

I have 1.2.2 of Rails.

Maybe it could be a job for a plugin to turn the installation into a UK version, just thinking out loud.

Re: Currencies

There's no UK version of Rails from what I'm aware of, so freeze Rails will not solve your problem.

I'm assuming you want the number_to_currency method to default to pounds so you don't need to specify ":unit" every time?

You can create a new helper method to do this. Place this in your ApplicationHelper file:

# in helpers/application_helper.rb
def number_to_pounds(value, options = {})
  options.reverse_merge! :unit => '&pound;'
  number_to_currency(value, options)
end

Now you can call number_to_pounds instead of number_to_currency. You can also override the number_to_currency method itself, but that's a little more difficult.

Railscasts - Free Ruby on Rails Screencasts

Re: Currencies

Thanks, thats great.