rails one search field handled by two controllers












0















In my app I want to set a search form with two buttons. Each one of those buttons should send request to separate controller. Something like this:



<%= form_tag(products_path, method: :get) do %>
Search Field <%= text_field_tag :q %>
<br>
<%= submit_tag 'First controller' %>
<%= submit_tag 'Second Controller' %>
<% end %>


Is this even possible? Or rails just impose on developer an "one form - one controller" way?










share|improve this question























  • Hi, can you please explain why you want this scenario first place.

    – Vishal Taj PM
    Nov 24 '18 at 2:12











  • As @VishalTajPM said, I can't really see a use case for having two buttons for the same form in order to search 2 separate tables. Either have two seperate search forms or look into implementing global search.

    – Mark Merritt
    Nov 24 '18 at 2:14











  • I think from the UX perspective it is much more convenient to use one search bar instead of two separated onces. Since I want to browse the same model records but with different status field.

    – andrzej541
    Nov 24 '18 at 2:26











  • @andrzej541 then i would suggest you to update the question with form view for better understanding. in both case we cannot have 2 actions in one form and please explain this status field also.

    – Vishal Taj PM
    Nov 24 '18 at 2:30






  • 2





    Do it in JavaScript or have one controller search different things depending on which button was pressed.

    – mu is too short
    Nov 24 '18 at 2:42
















0















In my app I want to set a search form with two buttons. Each one of those buttons should send request to separate controller. Something like this:



<%= form_tag(products_path, method: :get) do %>
Search Field <%= text_field_tag :q %>
<br>
<%= submit_tag 'First controller' %>
<%= submit_tag 'Second Controller' %>
<% end %>


Is this even possible? Or rails just impose on developer an "one form - one controller" way?










share|improve this question























  • Hi, can you please explain why you want this scenario first place.

    – Vishal Taj PM
    Nov 24 '18 at 2:12











  • As @VishalTajPM said, I can't really see a use case for having two buttons for the same form in order to search 2 separate tables. Either have two seperate search forms or look into implementing global search.

    – Mark Merritt
    Nov 24 '18 at 2:14











  • I think from the UX perspective it is much more convenient to use one search bar instead of two separated onces. Since I want to browse the same model records but with different status field.

    – andrzej541
    Nov 24 '18 at 2:26











  • @andrzej541 then i would suggest you to update the question with form view for better understanding. in both case we cannot have 2 actions in one form and please explain this status field also.

    – Vishal Taj PM
    Nov 24 '18 at 2:30






  • 2





    Do it in JavaScript or have one controller search different things depending on which button was pressed.

    – mu is too short
    Nov 24 '18 at 2:42














0












0








0








In my app I want to set a search form with two buttons. Each one of those buttons should send request to separate controller. Something like this:



<%= form_tag(products_path, method: :get) do %>
Search Field <%= text_field_tag :q %>
<br>
<%= submit_tag 'First controller' %>
<%= submit_tag 'Second Controller' %>
<% end %>


Is this even possible? Or rails just impose on developer an "one form - one controller" way?










share|improve this question














In my app I want to set a search form with two buttons. Each one of those buttons should send request to separate controller. Something like this:



<%= form_tag(products_path, method: :get) do %>
Search Field <%= text_field_tag :q %>
<br>
<%= submit_tag 'First controller' %>
<%= submit_tag 'Second Controller' %>
<% end %>


Is this even possible? Or rails just impose on developer an "one form - one controller" way?







ruby-on-rails ruby ruby-on-rails-5






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 1:49









andrzej541andrzej541

9710




9710













  • Hi, can you please explain why you want this scenario first place.

    – Vishal Taj PM
    Nov 24 '18 at 2:12











  • As @VishalTajPM said, I can't really see a use case for having two buttons for the same form in order to search 2 separate tables. Either have two seperate search forms or look into implementing global search.

    – Mark Merritt
    Nov 24 '18 at 2:14











  • I think from the UX perspective it is much more convenient to use one search bar instead of two separated onces. Since I want to browse the same model records but with different status field.

    – andrzej541
    Nov 24 '18 at 2:26











  • @andrzej541 then i would suggest you to update the question with form view for better understanding. in both case we cannot have 2 actions in one form and please explain this status field also.

    – Vishal Taj PM
    Nov 24 '18 at 2:30






  • 2





    Do it in JavaScript or have one controller search different things depending on which button was pressed.

    – mu is too short
    Nov 24 '18 at 2:42



















  • Hi, can you please explain why you want this scenario first place.

    – Vishal Taj PM
    Nov 24 '18 at 2:12











  • As @VishalTajPM said, I can't really see a use case for having two buttons for the same form in order to search 2 separate tables. Either have two seperate search forms or look into implementing global search.

    – Mark Merritt
    Nov 24 '18 at 2:14











  • I think from the UX perspective it is much more convenient to use one search bar instead of two separated onces. Since I want to browse the same model records but with different status field.

    – andrzej541
    Nov 24 '18 at 2:26











  • @andrzej541 then i would suggest you to update the question with form view for better understanding. in both case we cannot have 2 actions in one form and please explain this status field also.

    – Vishal Taj PM
    Nov 24 '18 at 2:30






  • 2





    Do it in JavaScript or have one controller search different things depending on which button was pressed.

    – mu is too short
    Nov 24 '18 at 2:42

















Hi, can you please explain why you want this scenario first place.

– Vishal Taj PM
Nov 24 '18 at 2:12





Hi, can you please explain why you want this scenario first place.

– Vishal Taj PM
Nov 24 '18 at 2:12













As @VishalTajPM said, I can't really see a use case for having two buttons for the same form in order to search 2 separate tables. Either have two seperate search forms or look into implementing global search.

– Mark Merritt
Nov 24 '18 at 2:14





As @VishalTajPM said, I can't really see a use case for having two buttons for the same form in order to search 2 separate tables. Either have two seperate search forms or look into implementing global search.

– Mark Merritt
Nov 24 '18 at 2:14













I think from the UX perspective it is much more convenient to use one search bar instead of two separated onces. Since I want to browse the same model records but with different status field.

– andrzej541
Nov 24 '18 at 2:26





I think from the UX perspective it is much more convenient to use one search bar instead of two separated onces. Since I want to browse the same model records but with different status field.

– andrzej541
Nov 24 '18 at 2:26













@andrzej541 then i would suggest you to update the question with form view for better understanding. in both case we cannot have 2 actions in one form and please explain this status field also.

– Vishal Taj PM
Nov 24 '18 at 2:30





@andrzej541 then i would suggest you to update the question with form view for better understanding. in both case we cannot have 2 actions in one form and please explain this status field also.

– Vishal Taj PM
Nov 24 '18 at 2:30




2




2





Do it in JavaScript or have one controller search different things depending on which button was pressed.

– mu is too short
Nov 24 '18 at 2:42





Do it in JavaScript or have one controller search different things depending on which button was pressed.

– mu is too short
Nov 24 '18 at 2:42












3 Answers
3






active

oldest

votes


















2














Use JavaScript to change the form URL based on the button clicked.



<%= form_tag(first_controller_path,id: 'search-form', method: :get) do %>
Search Field <%= text_field_tag :q %>
<br>
<button type='submit' id="form-submit-button">First Controller</button>
<button type='button' id="second-controller-button">Second Controller</button>
<% end %>

<script>
$(function(){
$("#second-controller-button").on("click",function(e){
e.preventDefault();
var form = document.getElementById('search-form');
form.action = '<%= second_controller_path %>' ;
form.submit(); // Or you could also try document.getElementById("form-submit-button").click();
})
});
</script>





share|improve this answer

































    2














    HTML5 added some attributes to INPUT and BUTTON elements. One of them is formaction so you can set the action triggered by each button independantly and it overrides the default form action.



    <%= form_tag(products_path, method: :get) do %>
    Search Field <%= text_field_tag :q %>
    <br>
    <%= submit_tag 'First controller' #triggers the default action %>
    <%= submit_tag 'Second Controller', formaction: another_path %>
    <% end %>


    https://www.w3schools.com/tags/att_input_formaction.asp






    share|improve this answer
























    • This answer would be better if you used a reputable source like MDN instead of the clickbait w3schools.

      – max
      Nov 24 '18 at 15:53













    • @arieljuod This is a better solution than mine. I didn't know this :)

      – Abhilash Reddy
      Nov 25 '18 at 10:43



















    1














    You already defined form_tag with products_path through which the controller method is already defined.


    So answering your question, you can't pass send requests to two different controllers with one form.


    If you want to pass some status with buttons try adding some attributes to the buttons and differentiate them inside the controller.






    share|improve this answer





















    • 1





      You need to understand one thing here, The OP didn't want to submit the form to 2 different actions simultaneously. Based on the button clicked he wanted to change the action which is pretty much possible with the help of JS.

      – Abhilash Reddy
      Nov 24 '18 at 6:10






    • 1





      @AbhilashReddy You are right. Then your answer seems convincing. Great!

      – Vigneshwaran Sivalingam
      Nov 24 '18 at 6:18











    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%2f53454518%2frails-one-search-field-handled-by-two-controllers%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Use JavaScript to change the form URL based on the button clicked.



    <%= form_tag(first_controller_path,id: 'search-form', method: :get) do %>
    Search Field <%= text_field_tag :q %>
    <br>
    <button type='submit' id="form-submit-button">First Controller</button>
    <button type='button' id="second-controller-button">Second Controller</button>
    <% end %>

    <script>
    $(function(){
    $("#second-controller-button").on("click",function(e){
    e.preventDefault();
    var form = document.getElementById('search-form');
    form.action = '<%= second_controller_path %>' ;
    form.submit(); // Or you could also try document.getElementById("form-submit-button").click();
    })
    });
    </script>





    share|improve this answer






























      2














      Use JavaScript to change the form URL based on the button clicked.



      <%= form_tag(first_controller_path,id: 'search-form', method: :get) do %>
      Search Field <%= text_field_tag :q %>
      <br>
      <button type='submit' id="form-submit-button">First Controller</button>
      <button type='button' id="second-controller-button">Second Controller</button>
      <% end %>

      <script>
      $(function(){
      $("#second-controller-button").on("click",function(e){
      e.preventDefault();
      var form = document.getElementById('search-form');
      form.action = '<%= second_controller_path %>' ;
      form.submit(); // Or you could also try document.getElementById("form-submit-button").click();
      })
      });
      </script>





      share|improve this answer




























        2












        2








        2







        Use JavaScript to change the form URL based on the button clicked.



        <%= form_tag(first_controller_path,id: 'search-form', method: :get) do %>
        Search Field <%= text_field_tag :q %>
        <br>
        <button type='submit' id="form-submit-button">First Controller</button>
        <button type='button' id="second-controller-button">Second Controller</button>
        <% end %>

        <script>
        $(function(){
        $("#second-controller-button").on("click",function(e){
        e.preventDefault();
        var form = document.getElementById('search-form');
        form.action = '<%= second_controller_path %>' ;
        form.submit(); // Or you could also try document.getElementById("form-submit-button").click();
        })
        });
        </script>





        share|improve this answer















        Use JavaScript to change the form URL based on the button clicked.



        <%= form_tag(first_controller_path,id: 'search-form', method: :get) do %>
        Search Field <%= text_field_tag :q %>
        <br>
        <button type='submit' id="form-submit-button">First Controller</button>
        <button type='button' id="second-controller-button">Second Controller</button>
        <% end %>

        <script>
        $(function(){
        $("#second-controller-button").on("click",function(e){
        e.preventDefault();
        var form = document.getElementById('search-form');
        form.action = '<%= second_controller_path %>' ;
        form.submit(); // Or you could also try document.getElementById("form-submit-button").click();
        })
        });
        </script>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 6:14

























        answered Nov 24 '18 at 6:06









        Abhilash ReddyAbhilash Reddy

        1,1401618




        1,1401618

























            2














            HTML5 added some attributes to INPUT and BUTTON elements. One of them is formaction so you can set the action triggered by each button independantly and it overrides the default form action.



            <%= form_tag(products_path, method: :get) do %>
            Search Field <%= text_field_tag :q %>
            <br>
            <%= submit_tag 'First controller' #triggers the default action %>
            <%= submit_tag 'Second Controller', formaction: another_path %>
            <% end %>


            https://www.w3schools.com/tags/att_input_formaction.asp






            share|improve this answer
























            • This answer would be better if you used a reputable source like MDN instead of the clickbait w3schools.

              – max
              Nov 24 '18 at 15:53













            • @arieljuod This is a better solution than mine. I didn't know this :)

              – Abhilash Reddy
              Nov 25 '18 at 10:43
















            2














            HTML5 added some attributes to INPUT and BUTTON elements. One of them is formaction so you can set the action triggered by each button independantly and it overrides the default form action.



            <%= form_tag(products_path, method: :get) do %>
            Search Field <%= text_field_tag :q %>
            <br>
            <%= submit_tag 'First controller' #triggers the default action %>
            <%= submit_tag 'Second Controller', formaction: another_path %>
            <% end %>


            https://www.w3schools.com/tags/att_input_formaction.asp






            share|improve this answer
























            • This answer would be better if you used a reputable source like MDN instead of the clickbait w3schools.

              – max
              Nov 24 '18 at 15:53













            • @arieljuod This is a better solution than mine. I didn't know this :)

              – Abhilash Reddy
              Nov 25 '18 at 10:43














            2












            2








            2







            HTML5 added some attributes to INPUT and BUTTON elements. One of them is formaction so you can set the action triggered by each button independantly and it overrides the default form action.



            <%= form_tag(products_path, method: :get) do %>
            Search Field <%= text_field_tag :q %>
            <br>
            <%= submit_tag 'First controller' #triggers the default action %>
            <%= submit_tag 'Second Controller', formaction: another_path %>
            <% end %>


            https://www.w3schools.com/tags/att_input_formaction.asp






            share|improve this answer













            HTML5 added some attributes to INPUT and BUTTON elements. One of them is formaction so you can set the action triggered by each button independantly and it overrides the default form action.



            <%= form_tag(products_path, method: :get) do %>
            Search Field <%= text_field_tag :q %>
            <br>
            <%= submit_tag 'First controller' #triggers the default action %>
            <%= submit_tag 'Second Controller', formaction: another_path %>
            <% end %>


            https://www.w3schools.com/tags/att_input_formaction.asp







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 15:39









            arieljuodarieljuod

            7,15411221




            7,15411221













            • This answer would be better if you used a reputable source like MDN instead of the clickbait w3schools.

              – max
              Nov 24 '18 at 15:53













            • @arieljuod This is a better solution than mine. I didn't know this :)

              – Abhilash Reddy
              Nov 25 '18 at 10:43



















            • This answer would be better if you used a reputable source like MDN instead of the clickbait w3schools.

              – max
              Nov 24 '18 at 15:53













            • @arieljuod This is a better solution than mine. I didn't know this :)

              – Abhilash Reddy
              Nov 25 '18 at 10:43

















            This answer would be better if you used a reputable source like MDN instead of the clickbait w3schools.

            – max
            Nov 24 '18 at 15:53







            This answer would be better if you used a reputable source like MDN instead of the clickbait w3schools.

            – max
            Nov 24 '18 at 15:53















            @arieljuod This is a better solution than mine. I didn't know this :)

            – Abhilash Reddy
            Nov 25 '18 at 10:43





            @arieljuod This is a better solution than mine. I didn't know this :)

            – Abhilash Reddy
            Nov 25 '18 at 10:43











            1














            You already defined form_tag with products_path through which the controller method is already defined.


            So answering your question, you can't pass send requests to two different controllers with one form.


            If you want to pass some status with buttons try adding some attributes to the buttons and differentiate them inside the controller.






            share|improve this answer





















            • 1





              You need to understand one thing here, The OP didn't want to submit the form to 2 different actions simultaneously. Based on the button clicked he wanted to change the action which is pretty much possible with the help of JS.

              – Abhilash Reddy
              Nov 24 '18 at 6:10






            • 1





              @AbhilashReddy You are right. Then your answer seems convincing. Great!

              – Vigneshwaran Sivalingam
              Nov 24 '18 at 6:18
















            1














            You already defined form_tag with products_path through which the controller method is already defined.


            So answering your question, you can't pass send requests to two different controllers with one form.


            If you want to pass some status with buttons try adding some attributes to the buttons and differentiate them inside the controller.






            share|improve this answer





















            • 1





              You need to understand one thing here, The OP didn't want to submit the form to 2 different actions simultaneously. Based on the button clicked he wanted to change the action which is pretty much possible with the help of JS.

              – Abhilash Reddy
              Nov 24 '18 at 6:10






            • 1





              @AbhilashReddy You are right. Then your answer seems convincing. Great!

              – Vigneshwaran Sivalingam
              Nov 24 '18 at 6:18














            1












            1








            1







            You already defined form_tag with products_path through which the controller method is already defined.


            So answering your question, you can't pass send requests to two different controllers with one form.


            If you want to pass some status with buttons try adding some attributes to the buttons and differentiate them inside the controller.






            share|improve this answer















            You already defined form_tag with products_path through which the controller method is already defined.


            So answering your question, you can't pass send requests to two different controllers with one form.


            If you want to pass some status with buttons try adding some attributes to the buttons and differentiate them inside the controller.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 '18 at 8:31









            Danilo Cândido

            291414




            291414










            answered Nov 24 '18 at 6:06









            Vigneshwaran SivalingamVigneshwaran Sivalingam

            17118




            17118








            • 1





              You need to understand one thing here, The OP didn't want to submit the form to 2 different actions simultaneously. Based on the button clicked he wanted to change the action which is pretty much possible with the help of JS.

              – Abhilash Reddy
              Nov 24 '18 at 6:10






            • 1





              @AbhilashReddy You are right. Then your answer seems convincing. Great!

              – Vigneshwaran Sivalingam
              Nov 24 '18 at 6:18














            • 1





              You need to understand one thing here, The OP didn't want to submit the form to 2 different actions simultaneously. Based on the button clicked he wanted to change the action which is pretty much possible with the help of JS.

              – Abhilash Reddy
              Nov 24 '18 at 6:10






            • 1





              @AbhilashReddy You are right. Then your answer seems convincing. Great!

              – Vigneshwaran Sivalingam
              Nov 24 '18 at 6:18








            1




            1





            You need to understand one thing here, The OP didn't want to submit the form to 2 different actions simultaneously. Based on the button clicked he wanted to change the action which is pretty much possible with the help of JS.

            – Abhilash Reddy
            Nov 24 '18 at 6:10





            You need to understand one thing here, The OP didn't want to submit the form to 2 different actions simultaneously. Based on the button clicked he wanted to change the action which is pretty much possible with the help of JS.

            – Abhilash Reddy
            Nov 24 '18 at 6:10




            1




            1





            @AbhilashReddy You are right. Then your answer seems convincing. Great!

            – Vigneshwaran Sivalingam
            Nov 24 '18 at 6:18





            @AbhilashReddy You are right. Then your answer seems convincing. Great!

            – Vigneshwaran Sivalingam
            Nov 24 '18 at 6:18


















            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%2f53454518%2frails-one-search-field-handled-by-two-controllers%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin