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?
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>
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
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.
Related
This is what I'm hoping to do. Currently I have a drop down list of characteristics from which user can choose and search does certain element holds that characteristic. He picks a characteristic from drop down menu and clicks search button. Now I am trying to make a list of links for those characteristics so user can immediately click on certain characteristic.
I will have a couple of links separated with |:
characteristic1_link | characteristic2_link | characteristic3_link
Currently I have the following for drop down search which works:
<%= form_for(#element, method: 'get', url: 'query') do |f| %>
<%= f.collection_select :characteristic_id, Characteristic.all, :id, :name, :include_blank => true %>
<%= f.submit "Search" %>
<% end %>
I am trying to do that with links which I generate like this:
<% #characteristics.each do |characteristic| %>
<%= link_to (characteristic.name), '#' %>
<% end %>
How can I pass :characteristic_id parameter with link and somehow make f.submit to trigger when user clicks on link?
EDIT:
This seems to be working:
<% #characteristics.each do |characteristic| %>
<%= link_to (characteristic.name), query_path(:element => {:characteristic_id => characteristic.id}) %>
<% end %>
Opinions about this method? :)
if i understand your question like that clicking and link_to-anchor selects the option with the same name as the anchors label and submits the form then you might want to...
<%= link_to characteristic.name, '#', class: 'trigger_select' %>
and let your JS do the rest. (untested.)
$('.trigger_select').click( function() {
var href = $(this).attr('href');
$('#select_field').val(href);
$('#form').submit();
});
hope i understood you correcly.
Here's my form:
<%= form_for #asset do |f| %>
<%= f.check_box :remove_picture %>
<%= f.submit "Remove" %>
<% end %>
How could I just make this one button that does :remove_picture and submit? Thanks
Check out the API dock for the form_for helper:
http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
You can force the form to use the full array of HTTP verbs by setting
:method => (:get|:post|:put|:delete)
So your code might look like
<%= form_for(#asset, html: { method: :delete }) do |f| %>
<%= f.submit "Remove" %>
<% end %>
You could change the checkbox to a hidden field on the form...
If it were me, I'd look at something like button_to and handle this via AJAX on the controller. This way the button would call a controller action, say remove_picture and return a JS response which could update your view.
Example:
button_to([remove_picture, #asset], { method: :delete })
Note: method: :delete may not be needed - depends on your routes.
I need to include an external search form in my main layout view, such that it is displayed with every page render. At the moment, in my layouts/main.html.erb I have:
<div style="float: right";>
<% form_tag url_for("http://search.example.co.uk/search"), {:method => :get} do %>
<%= text_field_tag(:q) %>
<%= select_tag(:category, '<option value="all">All</option> <option value="dogs">Dogs</option> <option value="cats">Cats</option>') %>
<%= hidden_field_tag("site", "Example") %>
<%= hidden_field_tag("btnG", "Search") %>
<%= hidden_field_tag("filter", "0") %>
<%= hidden_field_tag("proxystylesheet", "std_stylesheet") %>
<%= submit_tag "Search" %>
<% end %>
</div>
This currently works, and in the header of all pages rendering the main layout, I have a text field, a drop down and a Search button. On clicking the 'Search' button, the page is directed to http://search.example.co.uk/search with all the parameters.
I need to be able to modify the parameters before the page is redirected. I need to concatenate params[:q] and params[:category] and pass this with the redirection. Is this possible?
Thanks for any help/suggestions.
You should do this by submitting your form to an action which would concatenate all your results and they would in-turn redirect your request to the URL which you have specified along with the parameters that you have processed...
I've been toying with using icons instead of standard buttons. I like the look of it and occasionally a icon is clearer than a labelled button.
While I found it easy to implement for link_to calls;
<%= link_to raw('<i class="icon-exclamation-sign"></i>'), '#' %>
I am struggling to achieve the same result when nested with form_for (or simple_form_for). Is there a way to use an icon for a submit or input when working with the form?
Here are some example forms from my code using the standard buttons:
<%= simple_form_for :present, url: present_path(list_item), method: 'put' do |f| %>
<%= f.hidden_field :purchased, value: "1" %>
<%= f.submit "owned" %>
<% end %>
<%= form_for :present, action: 'new', url: presents_path do |f| %>
<%= f.hidden_field :purchased, value: '0' %>
<%= f.submit "List", class: "btn btn-mini" %>
<% end %>
I've tried a few methods but none of yielded just the icon acting as a button - most render the button with either the raw HTML as its contents or the icon within the button.
The end result I'd like to be able to implement is just the icon acting as a click-able item (similar to the link_to result).
you probably want to look at image_submit_tag http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-image_submit_tag
Trying to create a select menu with items from a collection, so that upon selection of an item, and hitting submit, the user is taken to the "Show" action for that item...What I have is something like this:
<% form_tag("subjects/#{#subject.id}/state/:id", :method=>:get) do %>
<%= select_tag('state', options_from_collection_for_select(State.states, 'id', 'name'))%>
<%= submit_tag "go!" %>
<% end %>
What'd I like is for what's selected in the menu to fill in the :id parameter...(this is rails 2.3)
You can send the form to an action that redirect where you want:
<% form_tag("some_controller/redirection", :method=>:get) do %>
<%= select_tag('id', options_from_collection_for_select(State.states, 'id', 'name'))%>
<%= hidden_field_tag :subject_id, #subject.id %>
<%= submit_tag "go!" %>
<% end %>
in SomeController
def redirection
redirect to "subjects/#{#{params[:subject_id]}}/state/#{params[:id]}"
end
The structure of the URL that you are requesting can't be made using only an HTML form. It will require some Javascript:
$('#my_form').submit(function(){
window.location = '/subjects/' + $('#subject_id').val() + '/state/' + $('#state_id').val();
});