rails one search field handled by two controllers
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
add a comment |
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
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 thisstatus 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
add a comment |
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
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
ruby-on-rails ruby ruby-on-rails-5
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 thisstatus 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
add a comment |
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 thisstatus 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
add a comment |
3 Answers
3
active
oldest
votes
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>
add a comment |
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
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
add a comment |
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.
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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%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
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>
add a comment |
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>
add a comment |
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>
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>
edited Nov 24 '18 at 6:14
answered Nov 24 '18 at 6:06
Abhilash ReddyAbhilash Reddy
1,1401618
1,1401618
add a comment |
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454518%2frails-one-search-field-handled-by-two-controllers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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