conditionally contain arguments in url path in rails











up vote
3
down vote

favorite
1












I have a search function which allows a user to search for items. From the generated list, the user can add items to a list. After adding an item from the list, the page reloads and the user is returned to their current search list. However, currently, if a user has not searched for anything, and just adds an item of the top of the list (starting at 'A, for example), when the page reloads it includes a blank search query in the url. I know why, it is because of this line



redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])


I want to make the last argument in the redirect path a conditional one so that, if say the :search_term == nil || == "" || " " then the redirect_to only includes the first argument.



How would I best achieve this?



Thanks in advance!










share|improve this question


























    up vote
    3
    down vote

    favorite
    1












    I have a search function which allows a user to search for items. From the generated list, the user can add items to a list. After adding an item from the list, the page reloads and the user is returned to their current search list. However, currently, if a user has not searched for anything, and just adds an item of the top of the list (starting at 'A, for example), when the page reloads it includes a blank search query in the url. I know why, it is because of this line



    redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])


    I want to make the last argument in the redirect path a conditional one so that, if say the :search_term == nil || == "" || " " then the redirect_to only includes the first argument.



    How would I best achieve this?



    Thanks in advance!










    share|improve this question
























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      I have a search function which allows a user to search for items. From the generated list, the user can add items to a list. After adding an item from the list, the page reloads and the user is returned to their current search list. However, currently, if a user has not searched for anything, and just adds an item of the top of the list (starting at 'A, for example), when the page reloads it includes a blank search query in the url. I know why, it is because of this line



      redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])


      I want to make the last argument in the redirect path a conditional one so that, if say the :search_term == nil || == "" || " " then the redirect_to only includes the first argument.



      How would I best achieve this?



      Thanks in advance!










      share|improve this question













      I have a search function which allows a user to search for items. From the generated list, the user can add items to a list. After adding an item from the list, the page reloads and the user is returned to their current search list. However, currently, if a user has not searched for anything, and just adds an item of the top of the list (starting at 'A, for example), when the page reloads it includes a blank search query in the url. I know why, it is because of this line



      redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])


      I want to make the last argument in the redirect path a conditional one so that, if say the :search_term == nil || == "" || " " then the redirect_to only includes the first argument.



      How would I best achieve this?



      Thanks in advance!







      ruby-on-rails ruby






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 21 '14 at 19:57









      rico_mac

      478314




      478314
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You can do:



          if params[:search_term].present?
          redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
          else
          redirect_to admin_job_job_products_path(@job)
          end


          Or



          redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].presence)




          References:





          • .present? method documentation


          • .blank? method documentation






          share|improve this answer























          • The 2nd example doesn't work properly. Please update your answer to match the other user's answer redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
            – Jeremy Moritz
            yesterday


















          up vote
          1
          down vote













          The second example of MrYoshiji probably will not work, but you can write it short and simple this why:



          redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)


          So if the result will be nil rails will not include that param in the url






          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',
            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%2f24873604%2fconditionally-contain-arguments-in-url-path-in-rails%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








            up vote
            2
            down vote



            accepted










            You can do:



            if params[:search_term].present?
            redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
            else
            redirect_to admin_job_job_products_path(@job)
            end


            Or



            redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].presence)




            References:





            • .present? method documentation


            • .blank? method documentation






            share|improve this answer























            • The 2nd example doesn't work properly. Please update your answer to match the other user's answer redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
              – Jeremy Moritz
              yesterday















            up vote
            2
            down vote



            accepted










            You can do:



            if params[:search_term].present?
            redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
            else
            redirect_to admin_job_job_products_path(@job)
            end


            Or



            redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].presence)




            References:





            • .present? method documentation


            • .blank? method documentation






            share|improve this answer























            • The 2nd example doesn't work properly. Please update your answer to match the other user's answer redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
              – Jeremy Moritz
              yesterday













            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            You can do:



            if params[:search_term].present?
            redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
            else
            redirect_to admin_job_job_products_path(@job)
            end


            Or



            redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].presence)




            References:





            • .present? method documentation


            • .blank? method documentation






            share|improve this answer














            You can do:



            if params[:search_term].present?
            redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
            else
            redirect_to admin_job_job_products_path(@job)
            end


            Or



            redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].presence)




            References:





            • .present? method documentation


            • .blank? method documentation







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 19 hours ago

























            answered Jul 21 '14 at 20:01









            MrYoshiji

            44.1k89391




            44.1k89391












            • The 2nd example doesn't work properly. Please update your answer to match the other user's answer redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
              – Jeremy Moritz
              yesterday


















            • The 2nd example doesn't work properly. Please update your answer to match the other user's answer redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
              – Jeremy Moritz
              yesterday
















            The 2nd example doesn't work properly. Please update your answer to match the other user's answer redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
            – Jeremy Moritz
            yesterday




            The 2nd example doesn't work properly. Please update your answer to match the other user's answer redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
            – Jeremy Moritz
            yesterday












            up vote
            1
            down vote













            The second example of MrYoshiji probably will not work, but you can write it short and simple this why:



            redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)


            So if the result will be nil rails will not include that param in the url






            share|improve this answer

























              up vote
              1
              down vote













              The second example of MrYoshiji probably will not work, but you can write it short and simple this why:



              redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)


              So if the result will be nil rails will not include that param in the url






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                The second example of MrYoshiji probably will not work, but you can write it short and simple this why:



                redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)


                So if the result will be nil rails will not include that param in the url






                share|improve this answer












                The second example of MrYoshiji probably will not work, but you can write it short and simple this why:



                redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)


                So if the result will be nil rails will not include that param in the url







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 4 '17 at 20:21









                GEkk

                188312




                188312






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24873604%2fconditionally-contain-arguments-in-url-path-in-rails%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

                    Create new schema in PostgreSQL using DBeaver

                    Deepest pit of an array with Javascript: test on Codility

                    Costa Masnaga