How to implement Yes/No instead of Boolean for certain cases in Rails?

前端 未结 3 631
庸人自扰
庸人自扰 2021-01-02 21:47

I\'m a newb at programming but in my application I want certain cases to display \"yes\" or \"no\" instead of \"true\" or \"false\". I\'m not sure the best way to do this,

3条回答
  •  情深已故
    2021-01-02 22:16

    The simplest option to get setup is to create a helper method which you can put in the application helper.

    # in app/helpers/application_helper.rb
    
    def boolean_to_words(value)
      value ? "Yes" : "No"
    end
    

    This is analogous to many other Rails conversion helpers such as number_to_currency

提交回复
热议问题