AASMからRadioボタンを描画する
モデルのAASMはこう。
aasm column: "approving_state" do state :applied state :tested state :rejected state :approved, initial: true event :apply, after: :send_apply do transitions from: [:tested, :rejected, :approved], to: :applied end event :test, after: :send_test do transitions from: :applied, to: :tested end event :reject do transitions from: [:applied, :tested], to: :rejected end event :approve, after: :send_approve do transitions from: :tested, to: :approved end end
これをViewにて表示
<% v = [] Company.aasm.states.each do |s| next if s.state_machine.config.column.to_s != 'approving_state' state = s.name.to_s label = t("activerecord.attributes.company.approving_states.#{state}") event = '' s.state_machine.events.each_key do |k| if s.state_machine.events[k].transitions[0].to.to_s == state event = k.to_s break end end disabled = true disabled = false if @company.send("may_#{event}?") || @company.approving_state == state disabled = true if state == 'applied' && @company.approving_state != 'applied' checked = @company.approving_state == state v.push({:state => state, :label => label, :event => event, :disabled => disabled, :checked => checked}) end %> <% v.each do |r| %> <div class="radio-inline"> <%= f.radio_button :approving, r[:state], {:disabled => r[:disabled], :checked => r[:checked]} %><%= f.label "approving_#{r[:state]}".to_sym, r[:label] %> </div> <% end %>
却下されたけどね。