ActiveAdmin Collection Names












2















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?










share|improve this question





























    2















    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?










    share|improve this question



























      2












      2








      2








      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?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 13:08







      hashrocket

















      asked Nov 27 '18 at 5:01









      hashrockethashrocket

      782613




      782613
























          2 Answers
          2






          active

          oldest

          votes


















          2














          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]}





          share|improve this answer

































            0














            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"





            share|improve this answer

























              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              2














              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]}





              share|improve this answer






























                2














                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]}





                share|improve this answer




























                  2












                  2








                  2







                  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]}





                  share|improve this answer















                  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]}






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 28 '18 at 4:58

























                  answered Nov 27 '18 at 5:09









                  Gagan GamiGagan Gami

                  8,46111642




                  8,46111642

























                      0














                      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"





                      share|improve this answer






























                        0














                        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"





                        share|improve this answer




























                          0












                          0








                          0







                          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"





                          share|improve this answer















                          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"






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 27 '18 at 6:20

























                          answered Nov 27 '18 at 6:15









                          iGianiGian

                          4,2692623




                          4,2692623






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                              Calculate evaluation metrics using cross_val_predict sklearn

                              Insert data from modal to MySQL (multiple modal on website)