How to change a text field's contents to all uppercase letter

Hi,

I’m relatively new to RoR so pardon if this is a really stupid
question. I have a text field where people will be entering a
username. I need to figure out a way to either

  1. validate the field so that it will not be accepted unless they’ve
    entered all uppercase letters for their username – less ideal
  2. regardless of what they enter in the field (uppercase or lowercase)
    I need to convert the username value to uppercase before it’s stored
    in the database.

Here is my form:

<% form_for(@post) do |f| %>
<%= f.error_messages %>

Enter Details
<%= f.label :username,'Enter your username:' %> <%= f.text_field :username, :size => 40 %>
<%= f.submit "Save", :class => "submit" %>

<% end %>

Thanks so much for any assistance! I greatly appreciate any help.

  1. regardless of what they enter in the field (uppercase or lowercase)
    I need to convert the username value to uppercase before it’s stored

Try this:

class Post < ActiveRecord::Base
def before_save
self.username.downcase!
end
end

Cheers,

Andy

did you mean:

self.username.upcase ?? … I need to make sure the username is
uppercase, not lowercase.

Thanks so much for the quick reply!

  1. regardless of what they enter in the field (uppercase or lowercase)
    I need to convert the username value to uppercase before it’s stored
self.username.downcase!

erm… upcase! :wink:

Doh :slight_smile:

Or change the setter (rather than messing around with filters :wink:

def username=(value)
write_attribute(:username, value.upcase)
end

Is the setter called if you assign to the attributes hash (e.g. with new
or
create)?

I guess it must be, but I’d generally put that in a callback to be sure
:slight_smile:

Cheers,

Andy

On 5 March 2010 16:05, Andy J. [email protected] wrote:

  1. regardless of what they enter in the field (uppercase or lowercase)
    I need to convert the username value to uppercase before it’s stored
self.username.downcase!

erm… upcase! :wink:

Or change the setter (rather than messing around with filters :wink:

def username=(value)
write_attribute(:username, value.upcase)
end

I’d be putting this:

def username=(value)
write_attribute(:username, value.upcase)
end

in my controller right? Sorry for the beginning questions…

On 5 March 2010 16:17, Andy J. [email protected] wrote:

ooo… good question - don’t know without testing it…

User.new(:username => “fred”)
=> #<User id: nil, role_detail_id: nil, title: “FRED”, first_name:
nil, last_name: nil>

yup, looks that way.

But more to the point, I don’t like changing users’ input before
storing it in the DB. We’ve shown how you can do it, and you’ve said
you need to, but I’d wonder whether that’s really a want to…
either way… it works either way.

On 5 March 2010 16:24, xxdesmus [email protected] wrote:

I’d be putting this:

def username=(value)
write_attribute(:username, value.upcase)
end

in my controller right? Sorry for the beginning questions…

nope - in the User model.

Worked perfectly…thanks so much!

Hey, you don’t happen to know of a simple way of implementing a date
picker for a field in a form do you? I was hoping to accomplish this
without needing to track down a plugin for the task.

On a related note: how can I set the default value of a field to be
today’s date (just the date, no time value)

Thanks for all the help!

On 5 March 2010 16:43, Michael P. [email protected] wrote:

Again in the model (one of several ways to address the need) :

def after_initialize
my_date_field = Date.today
end

Sorry - that’s bad - it will always reset the value to today even if
there is an existing value…

This will just set it if the “my_date_field” value is nil:

my_date_field ||= Date.today

On 5 March 2010 16:38, xxdesmus [email protected] wrote:

Hey, you don’t happen to know of a simple way of implementing a date
picker for a field in a form do you? I was hoping to accomplish this
without needing to track down a plugin for the task.

I’ve used the “calendar_date_select” plugin in the past - it is a
plugin, but it’s also very simple.

On a related note: how can I set the default value of a field to be
today’s date (just the date, no time value)

Again in the model (one of several ways to address the need) :

def after_initialize
my_date_field = Date.today
end

When I add this to my model:

def after_initialize
:date = Date.today
end

I get this error – /home/xxdesmus/timetool/app/models/post.rb:38:
syntax error, unexpected ‘=’, expecting kEND :date = Date.today ^

You will do, you’re trying to assign a value to a symbol (which is an
immutable object).

when I try it this way:

def after_initialize
date = Date.today
end

It doesn’t do anything …probably because my field should have a
different name other than “date” huh?

def after_initialize
self.date = Date.today
end

Doing:

date =

Will just assign the value to a local variable.

Cheers,

Andy

When I add this to my model:

def after_initialize
:date = Date.today
end

I get this error – /home/xxdesmus/timetool/app/models/post.rb:38:
syntax error, unexpected ‘=’, expecting kEND :date = Date.today ^

when I try it this way:

def after_initialize
date = Date.today
end

It doesn’t do anything …probably because my field should have a
different name other than “date” huh?

On 5 March 2010 16:55, Andy J. [email protected] wrote:

def after_initialize
self.date = Date.today
end

Doing:
date =
Will just assign the value to a local variable.

+1
pesky local variables…

On 5 March 2010 16:52, xxdesmus [email protected] wrote:

When I add this to my model:

def after_initialize
:date = Date.today
end

As an aside, using “date” as a name for a db field might cause you
confusion down the line - it can be a reserved word (depending on the
db). I’d try to give the some more name context: “registration_date”
or whatever.

Got it working with:

def after_initialize
self.date ||= Date.today
end

…hopefully I won’t be pressing my luck, but one last question (for
now). So I have a bunch of form fields that I need to add up their
values, and make sure their sum does not except the value entered in a
new form field. If the sum does exceed the form field value then I
want to alert the user and prevent it from saving. Basically I want to
validate that :atWorkHours.value > ( :hoursMeetings.value

  • :hoursTraining.value + :hoursProjTrav.value )

I tried to do something similar to this in my model…but I assume I
just had the syntax completely wrong.

Last question/request for the day I promise!

Thanks,
Justin :slight_smile:

On 5 March 2010 17:02, xxdesmus [email protected] wrote:

So I have a bunch of form fields that I need to add up their
values, and make sure their sum does not except the value entered in a
new form field. If the sum does exceed the form field value then I
want to alert the user and prevent it from saving. Basically I want to
validate that :atWorkHours.value > ( :hoursMeetings.value

  • :hoursTraining.value + :hoursProjTrav.value )

I tried to do something similar to this in my model…but I assume I
just had the syntax completely wrong.

Assuming all of the “hours” attributes all return numbers (something,
somewhere is ensuring that, I hope :wink: – you can add some
validations to your model. Again, there’s a couple of ways of doing
this - I normally separate the method that adds the error to base from
the code that checks the condition (so I can check the condition
myself if I want), but you could easily merge them together if you
prefer:

validate :validate_work_hours_not_exceeded

def work_hours_exceeded?
# this is a little different to your equation, as yours would
raise an error if both sides were exactly the same, and I’d guess
that’s not the right result - but if not, adjust it to suit
[hours_at_meetings, hours_in_training, hours_travelling].sum >
hours_at_work
end

private
def validate_work_hours_not_exceeded
errors.add_to_base(“Do not exceed working hours”) if
work_hours_exceeded?
end

I took the liberty of tweaking the variable names (although you were
showing them as symbols…) to make them a little more in line with
conventions (and, I think, more easily readable - YMMV :slight_smile:

Not having any luck with Calendar Date Select so far … got it
installed, but it looks like the documentation is out of date @
http://www.railslodge.com/plugins/46-calendar-date-select …I can’t
for the life of me get the pop-up or embedded calendar to
display …let alone get either to display in a form to take a value.

Thank you so much for all your help!

I tried date_select but I kept getting a bunch of errors.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs