Downcasing & Upcasing in Ruby on Rails
I have built a ruby on rails app that allows for users to track their workouts. I also allow them (like a blog) to create tags for their workouts. When users add tags to their workouts, they are assisted through auto_complete to help keep things organized.
As the site has grown in popularity I am getting a little bit of clutter/inconsistency because of capitalization. Is there a way to tell the app to only create 开发者_如何学Ctag.name
's in uppercase or lowercase?
Try something like:
class Tag < AR::Base
before_save :downcase_name
private
def downcase_name
self.name.downcase!
end
end
Ruby has an upcase and downcase on the String class that will allow you to do either. Simply call before saving the data.
For example, if you are using the simple_auto_complete gem, you could do the following:
class BlogController < ApplicationController
autocomplete_for :post, :title do |items|
items.map {|x| x.upcase}
end
end
I can't test this right now, so forgive me if there are syntax errors.
精彩评论