Using enum with select in a form_tag












0














user.rb



enum gender_type: [:male, :female] 
scope :gender, -> (gender_type) { where gender_type: gender_type}


userscontroller



@users = User.gender(params[:gender_type]).paginate(page: params[:page]) if params[:gender_type].present? 


index.html.erb



<%= form_tag users_path, method: :get do %>
<%= select_tag ?????? %>
<%= submit_tag "Search", :name => "nil", :id => "submit-gender" %>
<% end %>


the goal is to end up with:




example.com/users?gender_type=0 or example.com/users?gender_type=1











share|improve this question





























    0














    user.rb



    enum gender_type: [:male, :female] 
    scope :gender, -> (gender_type) { where gender_type: gender_type}


    userscontroller



    @users = User.gender(params[:gender_type]).paginate(page: params[:page]) if params[:gender_type].present? 


    index.html.erb



    <%= form_tag users_path, method: :get do %>
    <%= select_tag ?????? %>
    <%= submit_tag "Search", :name => "nil", :id => "submit-gender" %>
    <% end %>


    the goal is to end up with:




    example.com/users?gender_type=0 or example.com/users?gender_type=1











    share|improve this question



























      0












      0








      0







      user.rb



      enum gender_type: [:male, :female] 
      scope :gender, -> (gender_type) { where gender_type: gender_type}


      userscontroller



      @users = User.gender(params[:gender_type]).paginate(page: params[:page]) if params[:gender_type].present? 


      index.html.erb



      <%= form_tag users_path, method: :get do %>
      <%= select_tag ?????? %>
      <%= submit_tag "Search", :name => "nil", :id => "submit-gender" %>
      <% end %>


      the goal is to end up with:




      example.com/users?gender_type=0 or example.com/users?gender_type=1











      share|improve this question















      user.rb



      enum gender_type: [:male, :female] 
      scope :gender, -> (gender_type) { where gender_type: gender_type}


      userscontroller



      @users = User.gender(params[:gender_type]).paginate(page: params[:page]) if params[:gender_type].present? 


      index.html.erb



      <%= form_tag users_path, method: :get do %>
      <%= select_tag ?????? %>
      <%= submit_tag "Search", :name => "nil", :id => "submit-gender" %>
      <% end %>


      the goal is to end up with:




      example.com/users?gender_type=0 or example.com/users?gender_type=1








      ruby-on-rails enums






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 4 '16 at 6:08







      Timmy Von Heiss

















      asked Sep 4 '16 at 4:47









      Timmy Von Heiss Timmy Von Heiss

      1,003718




      1,003718
























          4 Answers
          4






          active

          oldest

          votes


















          1














          To get the mappings from a enum attribute you can use the pluralised version of the the enum attribute name:



          User. gender_types 
          => { male: 0, female: 1 }


          You can call Hash.to_a to get an array of pairs that you can pass to options_for_select. But you may want to use .map to transform the keys.



          class User
          self.gender_options
          # or use the I18n module to humanize the keys
          self.gender_types.map { |k,v| [k.capitalize, v] }
          end
          end

          <%= select_tag 'gender', options_for_select(User.gender_options) %>





          share|improve this answer























          • thanks. it should be self.gender_types.map and :gender_type
            – Timmy Von Heiss
            Sep 4 '16 at 6:27





















          1














          Try that kind



          select_tag 'gender', "<option value=0>male</option><option value=1>female</option>".html_safe


          or smth like that



          select_tag 'gender', options_for_select([["male",0],["female",1]])


          and you can read rails api to find the solution
          http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag






          share|improve this answer

















          • 1




            thanks. yeah, i was kind of curious though how to do it without manually listing them (if possible) for future reference since there might be much more than two options next time. i will accept this though if no one comes along with that answer.
            – Timmy Von Heiss
            Sep 4 '16 at 6:07



















          1














          (I assume you mistyped in filename users.rb and it is a regular user.rb model)



          User.gender_types will return a hash {"male" => 0, "female" => 1}. Sounds easy!



          select_tag :gender_type, options_for_select(User.gender_types)






          share|improve this answer























          • I am getting this error. ActionView::Template::Error (undefined method statuses for #<Class:0x007f4199c56660>): Also it should be :gender_type not :gender
            – Timmy Von Heiss
            Sep 4 '16 at 6:18












          • Oh right, my bad, updated the answer. You need to call pluralised version of the word you pass into enum. You want to run User.gender_types.
            – shlajin
            Sep 4 '16 at 6:20










          • this resulted in {"male" => 0, "female" => 1} being passed into the selection options. max solved it. :)
            – Timmy Von Heiss
            Sep 4 '16 at 6:29










          • sigh I should have paid more attention here. Updated the answer again, forgot the options_for_select call.
            – shlajin
            Sep 4 '16 at 6:32



















          0














          You can simply use like this



          <%= form_for(user) do |form| %>
          <%= form.select :gender_types, User.gender_types.keys.to_a %>
          <% end %>


          But assuming you use Rails Internationalization I18n (which I recommend) You may want to translate this enum like this in your view helper (or :



          # app/helpers/users_helper.rb
          def genders_select
          User.gender_types.map do |k,v|
          [User.human_enum_name(:gender_types, k), v]
          end
          end


          and



          <%= form_for(user) do |form| %>
          <%= form.select :gender_types, @genders_select %>
          <% end %>





          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%2f39313639%2fusing-enum-with-select-in-a-form-tag%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            To get the mappings from a enum attribute you can use the pluralised version of the the enum attribute name:



            User. gender_types 
            => { male: 0, female: 1 }


            You can call Hash.to_a to get an array of pairs that you can pass to options_for_select. But you may want to use .map to transform the keys.



            class User
            self.gender_options
            # or use the I18n module to humanize the keys
            self.gender_types.map { |k,v| [k.capitalize, v] }
            end
            end

            <%= select_tag 'gender', options_for_select(User.gender_options) %>





            share|improve this answer























            • thanks. it should be self.gender_types.map and :gender_type
              – Timmy Von Heiss
              Sep 4 '16 at 6:27


















            1














            To get the mappings from a enum attribute you can use the pluralised version of the the enum attribute name:



            User. gender_types 
            => { male: 0, female: 1 }


            You can call Hash.to_a to get an array of pairs that you can pass to options_for_select. But you may want to use .map to transform the keys.



            class User
            self.gender_options
            # or use the I18n module to humanize the keys
            self.gender_types.map { |k,v| [k.capitalize, v] }
            end
            end

            <%= select_tag 'gender', options_for_select(User.gender_options) %>





            share|improve this answer























            • thanks. it should be self.gender_types.map and :gender_type
              – Timmy Von Heiss
              Sep 4 '16 at 6:27
















            1












            1








            1






            To get the mappings from a enum attribute you can use the pluralised version of the the enum attribute name:



            User. gender_types 
            => { male: 0, female: 1 }


            You can call Hash.to_a to get an array of pairs that you can pass to options_for_select. But you may want to use .map to transform the keys.



            class User
            self.gender_options
            # or use the I18n module to humanize the keys
            self.gender_types.map { |k,v| [k.capitalize, v] }
            end
            end

            <%= select_tag 'gender', options_for_select(User.gender_options) %>





            share|improve this answer














            To get the mappings from a enum attribute you can use the pluralised version of the the enum attribute name:



            User. gender_types 
            => { male: 0, female: 1 }


            You can call Hash.to_a to get an array of pairs that you can pass to options_for_select. But you may want to use .map to transform the keys.



            class User
            self.gender_options
            # or use the I18n module to humanize the keys
            self.gender_types.map { |k,v| [k.capitalize, v] }
            end
            end

            <%= select_tag 'gender', options_for_select(User.gender_options) %>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 4 '16 at 6:51

























            answered Sep 4 '16 at 6:18









            maxmax

            44.9k857103




            44.9k857103












            • thanks. it should be self.gender_types.map and :gender_type
              – Timmy Von Heiss
              Sep 4 '16 at 6:27




















            • thanks. it should be self.gender_types.map and :gender_type
              – Timmy Von Heiss
              Sep 4 '16 at 6:27


















            thanks. it should be self.gender_types.map and :gender_type
            – Timmy Von Heiss
            Sep 4 '16 at 6:27






            thanks. it should be self.gender_types.map and :gender_type
            – Timmy Von Heiss
            Sep 4 '16 at 6:27















            1














            Try that kind



            select_tag 'gender', "<option value=0>male</option><option value=1>female</option>".html_safe


            or smth like that



            select_tag 'gender', options_for_select([["male",0],["female",1]])


            and you can read rails api to find the solution
            http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag






            share|improve this answer

















            • 1




              thanks. yeah, i was kind of curious though how to do it without manually listing them (if possible) for future reference since there might be much more than two options next time. i will accept this though if no one comes along with that answer.
              – Timmy Von Heiss
              Sep 4 '16 at 6:07
















            1














            Try that kind



            select_tag 'gender', "<option value=0>male</option><option value=1>female</option>".html_safe


            or smth like that



            select_tag 'gender', options_for_select([["male",0],["female",1]])


            and you can read rails api to find the solution
            http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag






            share|improve this answer

















            • 1




              thanks. yeah, i was kind of curious though how to do it without manually listing them (if possible) for future reference since there might be much more than two options next time. i will accept this though if no one comes along with that answer.
              – Timmy Von Heiss
              Sep 4 '16 at 6:07














            1












            1








            1






            Try that kind



            select_tag 'gender', "<option value=0>male</option><option value=1>female</option>".html_safe


            or smth like that



            select_tag 'gender', options_for_select([["male",0],["female",1]])


            and you can read rails api to find the solution
            http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag






            share|improve this answer












            Try that kind



            select_tag 'gender', "<option value=0>male</option><option value=1>female</option>".html_safe


            or smth like that



            select_tag 'gender', options_for_select([["male",0],["female",1]])


            and you can read rails api to find the solution
            http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 4 '16 at 6:02









            gmrashgmrash

            512312




            512312








            • 1




              thanks. yeah, i was kind of curious though how to do it without manually listing them (if possible) for future reference since there might be much more than two options next time. i will accept this though if no one comes along with that answer.
              – Timmy Von Heiss
              Sep 4 '16 at 6:07














            • 1




              thanks. yeah, i was kind of curious though how to do it without manually listing them (if possible) for future reference since there might be much more than two options next time. i will accept this though if no one comes along with that answer.
              – Timmy Von Heiss
              Sep 4 '16 at 6:07








            1




            1




            thanks. yeah, i was kind of curious though how to do it without manually listing them (if possible) for future reference since there might be much more than two options next time. i will accept this though if no one comes along with that answer.
            – Timmy Von Heiss
            Sep 4 '16 at 6:07




            thanks. yeah, i was kind of curious though how to do it without manually listing them (if possible) for future reference since there might be much more than two options next time. i will accept this though if no one comes along with that answer.
            – Timmy Von Heiss
            Sep 4 '16 at 6:07











            1














            (I assume you mistyped in filename users.rb and it is a regular user.rb model)



            User.gender_types will return a hash {"male" => 0, "female" => 1}. Sounds easy!



            select_tag :gender_type, options_for_select(User.gender_types)






            share|improve this answer























            • I am getting this error. ActionView::Template::Error (undefined method statuses for #<Class:0x007f4199c56660>): Also it should be :gender_type not :gender
              – Timmy Von Heiss
              Sep 4 '16 at 6:18












            • Oh right, my bad, updated the answer. You need to call pluralised version of the word you pass into enum. You want to run User.gender_types.
              – shlajin
              Sep 4 '16 at 6:20










            • this resulted in {"male" => 0, "female" => 1} being passed into the selection options. max solved it. :)
              – Timmy Von Heiss
              Sep 4 '16 at 6:29










            • sigh I should have paid more attention here. Updated the answer again, forgot the options_for_select call.
              – shlajin
              Sep 4 '16 at 6:32
















            1














            (I assume you mistyped in filename users.rb and it is a regular user.rb model)



            User.gender_types will return a hash {"male" => 0, "female" => 1}. Sounds easy!



            select_tag :gender_type, options_for_select(User.gender_types)






            share|improve this answer























            • I am getting this error. ActionView::Template::Error (undefined method statuses for #<Class:0x007f4199c56660>): Also it should be :gender_type not :gender
              – Timmy Von Heiss
              Sep 4 '16 at 6:18












            • Oh right, my bad, updated the answer. You need to call pluralised version of the word you pass into enum. You want to run User.gender_types.
              – shlajin
              Sep 4 '16 at 6:20










            • this resulted in {"male" => 0, "female" => 1} being passed into the selection options. max solved it. :)
              – Timmy Von Heiss
              Sep 4 '16 at 6:29










            • sigh I should have paid more attention here. Updated the answer again, forgot the options_for_select call.
              – shlajin
              Sep 4 '16 at 6:32














            1












            1








            1






            (I assume you mistyped in filename users.rb and it is a regular user.rb model)



            User.gender_types will return a hash {"male" => 0, "female" => 1}. Sounds easy!



            select_tag :gender_type, options_for_select(User.gender_types)






            share|improve this answer














            (I assume you mistyped in filename users.rb and it is a regular user.rb model)



            User.gender_types will return a hash {"male" => 0, "female" => 1}. Sounds easy!



            select_tag :gender_type, options_for_select(User.gender_types)







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 4 '16 at 6:32

























            answered Sep 4 '16 at 6:07









            shlajinshlajin

            1,101520




            1,101520












            • I am getting this error. ActionView::Template::Error (undefined method statuses for #<Class:0x007f4199c56660>): Also it should be :gender_type not :gender
              – Timmy Von Heiss
              Sep 4 '16 at 6:18












            • Oh right, my bad, updated the answer. You need to call pluralised version of the word you pass into enum. You want to run User.gender_types.
              – shlajin
              Sep 4 '16 at 6:20










            • this resulted in {"male" => 0, "female" => 1} being passed into the selection options. max solved it. :)
              – Timmy Von Heiss
              Sep 4 '16 at 6:29










            • sigh I should have paid more attention here. Updated the answer again, forgot the options_for_select call.
              – shlajin
              Sep 4 '16 at 6:32


















            • I am getting this error. ActionView::Template::Error (undefined method statuses for #<Class:0x007f4199c56660>): Also it should be :gender_type not :gender
              – Timmy Von Heiss
              Sep 4 '16 at 6:18












            • Oh right, my bad, updated the answer. You need to call pluralised version of the word you pass into enum. You want to run User.gender_types.
              – shlajin
              Sep 4 '16 at 6:20










            • this resulted in {"male" => 0, "female" => 1} being passed into the selection options. max solved it. :)
              – Timmy Von Heiss
              Sep 4 '16 at 6:29










            • sigh I should have paid more attention here. Updated the answer again, forgot the options_for_select call.
              – shlajin
              Sep 4 '16 at 6:32
















            I am getting this error. ActionView::Template::Error (undefined method statuses for #<Class:0x007f4199c56660>): Also it should be :gender_type not :gender
            – Timmy Von Heiss
            Sep 4 '16 at 6:18






            I am getting this error. ActionView::Template::Error (undefined method statuses for #<Class:0x007f4199c56660>): Also it should be :gender_type not :gender
            – Timmy Von Heiss
            Sep 4 '16 at 6:18














            Oh right, my bad, updated the answer. You need to call pluralised version of the word you pass into enum. You want to run User.gender_types.
            – shlajin
            Sep 4 '16 at 6:20




            Oh right, my bad, updated the answer. You need to call pluralised version of the word you pass into enum. You want to run User.gender_types.
            – shlajin
            Sep 4 '16 at 6:20












            this resulted in {"male" => 0, "female" => 1} being passed into the selection options. max solved it. :)
            – Timmy Von Heiss
            Sep 4 '16 at 6:29




            this resulted in {"male" => 0, "female" => 1} being passed into the selection options. max solved it. :)
            – Timmy Von Heiss
            Sep 4 '16 at 6:29












            sigh I should have paid more attention here. Updated the answer again, forgot the options_for_select call.
            – shlajin
            Sep 4 '16 at 6:32




            sigh I should have paid more attention here. Updated the answer again, forgot the options_for_select call.
            – shlajin
            Sep 4 '16 at 6:32











            0














            You can simply use like this



            <%= form_for(user) do |form| %>
            <%= form.select :gender_types, User.gender_types.keys.to_a %>
            <% end %>


            But assuming you use Rails Internationalization I18n (which I recommend) You may want to translate this enum like this in your view helper (or :



            # app/helpers/users_helper.rb
            def genders_select
            User.gender_types.map do |k,v|
            [User.human_enum_name(:gender_types, k), v]
            end
            end


            and



            <%= form_for(user) do |form| %>
            <%= form.select :gender_types, @genders_select %>
            <% end %>





            share|improve this answer


























              0














              You can simply use like this



              <%= form_for(user) do |form| %>
              <%= form.select :gender_types, User.gender_types.keys.to_a %>
              <% end %>


              But assuming you use Rails Internationalization I18n (which I recommend) You may want to translate this enum like this in your view helper (or :



              # app/helpers/users_helper.rb
              def genders_select
              User.gender_types.map do |k,v|
              [User.human_enum_name(:gender_types, k), v]
              end
              end


              and



              <%= form_for(user) do |form| %>
              <%= form.select :gender_types, @genders_select %>
              <% end %>





              share|improve this answer
























                0












                0








                0






                You can simply use like this



                <%= form_for(user) do |form| %>
                <%= form.select :gender_types, User.gender_types.keys.to_a %>
                <% end %>


                But assuming you use Rails Internationalization I18n (which I recommend) You may want to translate this enum like this in your view helper (or :



                # app/helpers/users_helper.rb
                def genders_select
                User.gender_types.map do |k,v|
                [User.human_enum_name(:gender_types, k), v]
                end
                end


                and



                <%= form_for(user) do |form| %>
                <%= form.select :gender_types, @genders_select %>
                <% end %>





                share|improve this answer












                You can simply use like this



                <%= form_for(user) do |form| %>
                <%= form.select :gender_types, User.gender_types.keys.to_a %>
                <% end %>


                But assuming you use Rails Internationalization I18n (which I recommend) You may want to translate this enum like this in your view helper (or :



                # app/helpers/users_helper.rb
                def genders_select
                User.gender_types.map do |k,v|
                [User.human_enum_name(:gender_types, k), v]
                end
                end


                and



                <%= form_for(user) do |form| %>
                <%= form.select :gender_types, @genders_select %>
                <% end %>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 21:17









                RousseauAlexandreRousseauAlexandre

                559814




                559814






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f39313639%2fusing-enum-with-select-in-a-form-tag%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)