Created
March 22, 2013 19:31
-
-
Save amiel/5224071 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
# t.string :status, default: 'submitted' | |
# Contrived example | |
def self.status_options | |
%w[approved rejected submitted] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserDecorator < Draper::Base | |
decorates :user | |
# I've recently refactored this pattern to the Olson gem. | |
# See: https://github.com/carnesmedia/olson for more information. | |
def self.status_options_for_select | |
User.status_options.map { |s| [s.humanize, s] } | |
end | |
def status | |
model.status.humanize | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveAdmin.register User do | |
# ... | |
form do | |
f.inputs do | |
# So, here's an example of the problem. `f.object.status` now returns "Submitted", | |
# which doesn't match the actual status option: "submitted". So, the select doesn't | |
# have the correct status selected. | |
# This is sort of subtle, but one of those not-so-obvious bugs with big potential problems. | |
f.input :status, collection: UserDecorator.status_options_for_select | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment