2

For Rails capitalization, when I use it with a string and try to manually capitalize something else in the sentence, it won't allow me to capitalize it.

eg. "how is New York".capitalize

"How is new york"

Is there anyway in Rails that allows me to ensure the first word is capitalized, but also gives flexibility that if the user capitalizes something else in the string, it will still apply?

1

1 Answer 1

2

if you are using rails 5+ you can use str.upcase_first

other wise the simplest solution will be

str = "how is New York" str[0] = str[0].upcase 
Sign up to request clarification or add additional context in comments.

5 Comments

Do you know how I can pull this off with an instance variable in Rails?
what you mean, you can do exactly same with instance variables also.
So if I put this in my ERB file - it just disappears completely - <%= @portfolio.description[0].upcase! %> - not sure what I'm doing wrong.
that change is not in place. i updated my answer with another solution.
if not using rails 5+ you will have to use @portfolio.description[0] = @portfolio.description[0].upcase in your controller or a helper

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.