1

I am working on a web app form and don't really know anything at all about rails. I mainly tweak html and css while the real devs do the hard work! But I am now trying to simply add a dollar sign - '$' to an input box that is created using rails. I have no clue how to do this and my google efforts have turned up fruitless.

this is the code for the input box

 ` <%= f.label :incomeAnnual, 'Annual income' %>
  <%= f.text_field :incomeAnnual, tabindex: 25 %>
  <br/>'

can anyone please help me?

chiccaboom
  • 307
  • 1
  • 3
  • 14
  • If you need to by default show the $ in the text box just use the value attribute as used in HTML. Just like this: `<%= f.text_field :incomeAnnual, tabindex: 25, :value => '$' %>` – Deepesh Feb 06 '15 at 01:06
  • thank you! and what if I wanted to add automatic commas to separate the numbers. for example $500,000 or $400,000,000? – chiccaboom Feb 06 '15 at 16:32
  • I am providing you a link which will help you: http://apidock.com/rails/ActionView/Helpers/NumberHelper/number_to_currency – Deepesh Feb 06 '15 at 16:44
  • thanks. I did actually see that document and have been trying to use :delimiter =>',' in the following way: <%= f.label :currentMortgage, 'Value of current mortgage' %> <%= f.text_field :currentMortgage, tabindex: 33, :value => '$', :delimiter => ',' %> am I doing something wrong? B/c it's not adding the commas. – chiccaboom Feb 06 '15 at 16:48
  • so your values will be from starting in the text box. Or you need to comma separate the values which the user enters dynamically? – Deepesh Feb 06 '15 at 16:51
  • I'd like it so that when someone enters numbers in the input box it automatically adds the commas for every thousand. So basically the user doesn't have to enter the comma – chiccaboom Feb 06 '15 at 16:56

4 Answers4

1

To add the dollar sign you can use the value attribute as specified. Now to change it into currency format, if it is a predefined value you can use the rails helper number_to_currency. More details:

http://apidock.com/rails/ActionView/Helpers/NumberHelper/number_to_currency

But if you need to dynamically ad the commas as the user enters the value in text box then you need to use javascript for it. You can use a number of ways for it:

Add commas for a number in input field while typing

How can I format numbers as money in JavaScript?

Also there is a simple method which I prefer i.e., toLocaleString() which can be applied to a number. So what you need to do is on keypress() you need to take the number and convert it into the format you need and put in the text box. Hope this helps.

Community
  • 1
  • 1
Deepesh
  • 6,138
  • 1
  • 24
  • 41
0

Assuming you want the $ to appear inside the text box, you could use either :value or :placeholder options on the text_field.

A placeholder will go away as the user starts typing, so you might prefer a value here.

For example:

:value=> '$'

or

:placeholder => '$'

See also http://apidock.com/rails/ActionView/Helpers/FormHelper/text_field

Will
  • 2,768
  • 1
  • 17
  • 15
0

You can use

 <%= f.text_field :incomeAnnual, :value=> "$ #{f.object.incomeAnnual}",tabindex: 25 %>
Hardik Hardiya
  • 837
  • 6
  • 14
0

Use the number_to_currency function. For example

<%= number_to_currency item.total_price %>
bish
  • 3,381
  • 9
  • 48
  • 69