Check for dash before applying titleize in Rails -
Check for dash before applying titleize in Rails -
i new ruby , rails , need know how check if string contains dash before applying titlelize.
@city = city.first :conditions => { :title => params[:city].titleize }
what need is:
@city = city.first :conditions => { :title => params[:city] }
and write apply titleize if @city
variable doesn't contain dash.
i solution added zachrose couple of weeks ago: https://gist.github.com/varyonic/ccda540c417a6bd49aec
def nice_title(phrase) homecoming phrase if phrase =~ /^-+$/ phrase.split('-').map { |part| if part.chars.count == part.bytes.count part.titleize else part.split(' ').map { |word| word.mb_chars.titleize }.join(' ') end }.join('-') end
ruby-on-rails-3
Comments
Post a Comment