Skip to content

How to use Crystal enum with the options_for_select? #1879

Closed Answered by zw963
zw963 asked this question in Q&A
Discussion options

You must be logged in to vote

It seem like works change code like this:

class University < BaseModel
  enum BatchLevel
    LevelOne_A
    LevelOne_B
    LevelTwo_A
    LevelTwo_B

    def display_name
      case self
      in LevelOne_A
        "一本A"
      in LevelOne_B
        "一本B"
      in LevelTwo_A
        "二本A"
      in LevelTwo_B
        "二本B"
      end
    end

    def self.checkbox_batch_level
      values = [] of Tuple(String, University::BatchLevel)

      BatchNumber.each { |bn| values << {bn.display_name, bn} }

      values
    end
  end

  table do
    column batch_level : University::BatchLevel
  end
end

Then use in page like this:

options_for_select(op.batch_level, University::BatchNumber.checkbox_bat…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by zw963
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant