Forum: Ruby on Rails Altering text field value on form load

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
JannaB (Guest)
on 2009-05-28 22:37
(Received via mailing list)
Suppose I have a model, 'Associate', and it has a string member,
'branch'. Sometimes branch can end with a percent sign (%) and
sometimes not.

I have a form to display the Associate data. In the branch field, I am
simply putting whatever text is in branch, before (to the left of) the
input box. In the input box itself, I either want to show the % sign
if branch ends in %, or, nothing.

How can I specify this? Below is my stab at it -- SUppose the branch
is '34%' , Of course, the way I have it, what shows in the text box is
the full branch of  34%, wherein, in this case, it should just be %.

<% form_for @associate do |f| %>
     <%= f.label @associate.branch.split("%") %><%=
f.text_field :branch %>
<% end %>

(In my model, I have a before_save. which will set branch to that
original portion of branch left of the %, plus whatever is in the text
box. In so doing, no one can ever bring themselves up the branch-
coding tree, see?) -JannaB
This topic is locked and can not be replied to.