ActiveAdmin Collection Names
I have an array of roles in my model:
ROLES = ['super_admin', 'user', 'user_admin']
I have an ActiveAdmin form that shows these roles:
input :roles, as: :check_boxes, collection: User::ROLES
I have to show the roles as humanized, capitalized names instead of snake case on the form:
Super Admin, Salesman, Sales Admin
but when one of them is selected, it has to be saved in snakecase.
I've tried this:
User::ROLES
.map { |r| "#{r.humanize}" }
.map { |r| r.split.map(&:capitalize).join(' ') }
but this saves the role as the humanized, capitalized form instead of the snake case form. How can I use the humanized, capitalized version of the words on the form, but save the snake case version?
ruby ruby-on-rails-5 activeadmin
add a comment |
I have an array of roles in my model:
ROLES = ['super_admin', 'user', 'user_admin']
I have an ActiveAdmin form that shows these roles:
input :roles, as: :check_boxes, collection: User::ROLES
I have to show the roles as humanized, capitalized names instead of snake case on the form:
Super Admin, Salesman, Sales Admin
but when one of them is selected, it has to be saved in snakecase.
I've tried this:
User::ROLES
.map { |r| "#{r.humanize}" }
.map { |r| r.split.map(&:capitalize).join(' ') }
but this saves the role as the humanized, capitalized form instead of the snake case form. How can I use the humanized, capitalized version of the words on the form, but save the snake case version?
ruby ruby-on-rails-5 activeadmin
add a comment |
I have an array of roles in my model:
ROLES = ['super_admin', 'user', 'user_admin']
I have an ActiveAdmin form that shows these roles:
input :roles, as: :check_boxes, collection: User::ROLES
I have to show the roles as humanized, capitalized names instead of snake case on the form:
Super Admin, Salesman, Sales Admin
but when one of them is selected, it has to be saved in snakecase.
I've tried this:
User::ROLES
.map { |r| "#{r.humanize}" }
.map { |r| r.split.map(&:capitalize).join(' ') }
but this saves the role as the humanized, capitalized form instead of the snake case form. How can I use the humanized, capitalized version of the words on the form, but save the snake case version?
ruby ruby-on-rails-5 activeadmin
I have an array of roles in my model:
ROLES = ['super_admin', 'user', 'user_admin']
I have an ActiveAdmin form that shows these roles:
input :roles, as: :check_boxes, collection: User::ROLES
I have to show the roles as humanized, capitalized names instead of snake case on the form:
Super Admin, Salesman, Sales Admin
but when one of them is selected, it has to be saved in snakecase.
I've tried this:
User::ROLES
.map { |r| "#{r.humanize}" }
.map { |r| r.split.map(&:capitalize).join(' ') }
but this saves the role as the humanized, capitalized form instead of the snake case form. How can I use the humanized, capitalized version of the words on the form, but save the snake case version?
ruby ruby-on-rails-5 activeadmin
ruby ruby-on-rails-5 activeadmin
edited Nov 27 '18 at 13:08
hashrocket
asked Nov 27 '18 at 5:01
hashrockethashrocket
782613
782613
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use Rails(Active Support) method String#titleize instead split.map(&:capitalize).join(' ')
If you want to store record in DB as snakecase
you can do below way: In dropdown option it will be displayed as titalize
but it's value will be set as snakecase
which will be stored in DB.
> roles.map{|e|[e.titleize, e]}
#=> [["Super Admin", "super_admin"], ["User", "user"], ["User Admin", "user_admin"]]
in activeadmin use:
input :roles, as: :check_boxes, collection: User::ROLES.map{|e|[e.titleize, e]}
add a comment |
If you want to use pure Ruby, one alternative could be:
'super_admin'.split('_').map(&:capitalize).join(' ')
#=> "Super Admin"
So, mapping your array:
roles.map { |str| [str.split('_').map(&:capitalize).join(' '), str] }
#=> [["Super Admin", "super_admin"], ["User", "user"], ["User Admin", "user_admin"]]
Backward (before_save
):
"Super Admin".downcase.gsub(' ', '_') #=> "super_admin"
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53493029%2factiveadmin-collection-names%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Rails(Active Support) method String#titleize instead split.map(&:capitalize).join(' ')
If you want to store record in DB as snakecase
you can do below way: In dropdown option it will be displayed as titalize
but it's value will be set as snakecase
which will be stored in DB.
> roles.map{|e|[e.titleize, e]}
#=> [["Super Admin", "super_admin"], ["User", "user"], ["User Admin", "user_admin"]]
in activeadmin use:
input :roles, as: :check_boxes, collection: User::ROLES.map{|e|[e.titleize, e]}
add a comment |
You can use Rails(Active Support) method String#titleize instead split.map(&:capitalize).join(' ')
If you want to store record in DB as snakecase
you can do below way: In dropdown option it will be displayed as titalize
but it's value will be set as snakecase
which will be stored in DB.
> roles.map{|e|[e.titleize, e]}
#=> [["Super Admin", "super_admin"], ["User", "user"], ["User Admin", "user_admin"]]
in activeadmin use:
input :roles, as: :check_boxes, collection: User::ROLES.map{|e|[e.titleize, e]}
add a comment |
You can use Rails(Active Support) method String#titleize instead split.map(&:capitalize).join(' ')
If you want to store record in DB as snakecase
you can do below way: In dropdown option it will be displayed as titalize
but it's value will be set as snakecase
which will be stored in DB.
> roles.map{|e|[e.titleize, e]}
#=> [["Super Admin", "super_admin"], ["User", "user"], ["User Admin", "user_admin"]]
in activeadmin use:
input :roles, as: :check_boxes, collection: User::ROLES.map{|e|[e.titleize, e]}
You can use Rails(Active Support) method String#titleize instead split.map(&:capitalize).join(' ')
If you want to store record in DB as snakecase
you can do below way: In dropdown option it will be displayed as titalize
but it's value will be set as snakecase
which will be stored in DB.
> roles.map{|e|[e.titleize, e]}
#=> [["Super Admin", "super_admin"], ["User", "user"], ["User Admin", "user_admin"]]
in activeadmin use:
input :roles, as: :check_boxes, collection: User::ROLES.map{|e|[e.titleize, e]}
edited Nov 28 '18 at 4:58
answered Nov 27 '18 at 5:09
Gagan GamiGagan Gami
8,46111642
8,46111642
add a comment |
add a comment |
If you want to use pure Ruby, one alternative could be:
'super_admin'.split('_').map(&:capitalize).join(' ')
#=> "Super Admin"
So, mapping your array:
roles.map { |str| [str.split('_').map(&:capitalize).join(' '), str] }
#=> [["Super Admin", "super_admin"], ["User", "user"], ["User Admin", "user_admin"]]
Backward (before_save
):
"Super Admin".downcase.gsub(' ', '_') #=> "super_admin"
add a comment |
If you want to use pure Ruby, one alternative could be:
'super_admin'.split('_').map(&:capitalize).join(' ')
#=> "Super Admin"
So, mapping your array:
roles.map { |str| [str.split('_').map(&:capitalize).join(' '), str] }
#=> [["Super Admin", "super_admin"], ["User", "user"], ["User Admin", "user_admin"]]
Backward (before_save
):
"Super Admin".downcase.gsub(' ', '_') #=> "super_admin"
add a comment |
If you want to use pure Ruby, one alternative could be:
'super_admin'.split('_').map(&:capitalize).join(' ')
#=> "Super Admin"
So, mapping your array:
roles.map { |str| [str.split('_').map(&:capitalize).join(' '), str] }
#=> [["Super Admin", "super_admin"], ["User", "user"], ["User Admin", "user_admin"]]
Backward (before_save
):
"Super Admin".downcase.gsub(' ', '_') #=> "super_admin"
If you want to use pure Ruby, one alternative could be:
'super_admin'.split('_').map(&:capitalize).join(' ')
#=> "Super Admin"
So, mapping your array:
roles.map { |str| [str.split('_').map(&:capitalize).join(' '), str] }
#=> [["Super Admin", "super_admin"], ["User", "user"], ["User Admin", "user_admin"]]
Backward (before_save
):
"Super Admin".downcase.gsub(' ', '_') #=> "super_admin"
edited Nov 27 '18 at 6:20
answered Nov 27 '18 at 6:15
iGianiGian
4,2692623
4,2692623
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53493029%2factiveadmin-collection-names%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown