Modifying parameters passed to external url form with Ruby on Rails - ruby-on-rails

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...

Related

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?
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.

Header Navbar Form URL issue in Rails

I have a search form in the header navbar so that it is always available to users, but I can't get it to send to the correct controller/action. It works correctly when already on the page for search results, when used from any other page it sends to the controller/action for the current page . I found a similar problem here Rails 4 Bootstrap Search Form in Navbar but I did not run into the issue in the same fashion, nor did it help me. Where am I going wrong?
The form in my header navbar
<ul class="navbar-form navbar-left">
<form class="form-inline">
<%= form_tag(search_path, method: :get) do %>
<%=select_tag :gender, options_for_select
([['Male & Female', 'All'],['Male', 'Male'],
['Female', 'Female']]) %>
<%= label_tag "Include unavailable" %>
<%= check_box_tag :unavailable %>
<%= submit_tag "Search", class: "btn btn-success navbar-btn" %>
<% end %>
</form>
</ul>
Routing
get '/search', to: 'searches#search'
I have tried building the form both as form_for and form_tag, as well as URL options with either to force the correct controller/action, to no avail. It only appends the form params to the current url.
What am I missing?
I think I see the issue now.
<form class="form-inline">
<%= form_tag(search_path, method: :get) do %>
is actually putting two form tags on your page. You want to get rid of the outer <form> tag and add a class: 'form-inline' to your form_tag call.
You'll want a name on that route too:
get '/search', to: 'searches#search', as: 'search'

Rails link_to query params - selecting in item on the next page

Right now I have a link_to. It looks like a button the the user. It's an upgrade button so it routes the user to the plans page.
I not only want to route the user to the plans page but I want to pre-select the plan that supports the feature they are upgrading for. I was told that I could pass a query parameter into the link_to? Is the true and what does this look like in my situation? Here is my code.
LINK_TO
<%= link_to edit_account_plan_path, class: "button-big reverse-blue" do %>
HTML
<div class="plan-control">
<%= radio_button_tag :plan_id, plan.id, row.current?, disabled: row.ineligible? %>
</div>
you can pass a query parameter back to your controller something like this:
<%= link_to edit_account_plan_path(feature: 'basic'), class: "button-big reverse-blue" do %>
Then in your controller action routed by edit_account_plan_path, you can set an instance variable which you can use to preselect on your view page:
def edit
#plan = Plan.find_by(feature: params[:feature])
end
Then on your view page:
<div class="plan-control">
<%= radio_button_tag :plan_id, 'Basic', #plan.feature == 'basic' %>
</div>

form_for was put inside fields_for

I'm having a problem with form_for and fields_for.
So, my problem is:
I had a form_for, and inside this form_for, I use a fields_for. Inside this fields_for, I use a form_tag (i used ajax for this form_tag).
But when I view the generated HTML, it didn't display form_tag, it only display form_for. And I didn't understand why.
Please explain for me, why it didn't display form_tag.
Here is my form_for:
<div class="row">
<%= form_for #real_estate, url: admin_real_estate_update_path do |f| %>
<%= f.fields_for(:client) do |client| %>
<%= text_field :real_estate, :assessment_start_at, value: #real_estate.assessment_start_at %>
<%= render partial: "admin/real_estate/form/assessment", locals: {real_estate_id: #real_estate.id} %>
<% end %>
<%= f.submit "Submut", class: "btn btn-primary"%>
<% end %>
</div>
Here is my form_for which i put inside fields_for:
<%= form_tag admin_search_assessment_path(real_estate_id), method: :post, remote: true do %>
<%= text_field_tag :company_name, params[:company_name] %>
<%= submit_tag "Submit" %>
<% end %>
And i tried to add <form></form> follow as:
<div class="row">
<%= form_for #real_estate, url: admin_real_estate_update_path do |f| %>
<form></form>
<%= f.fields_for(:client) do |client| %>
<%= text_field :real_estate, :assessment_start_at, value: #real_estate.assessment_start_at %>
<%= render partial: "admin/real_estate/form/assessment", locals: {real_estate_id: #real_estate.id} %>
<% end %>
<%= f.submit "Submut", class: "btn btn-primary"%>
<% end %>
</div>
And form_tag was display, but form_for didn't display.
Update:
So, i used $("form_2").submit(function() {....}); to solve this problem.
Actually, i still want to use form-nested.
Inside this fields_for, I use a form_tag (i used ajax for this form_tag)
N'est pas possible, mon ami.
--
Here's how it works...
form_for and form_tag both generate pure HTML forms:
<form action="/action" method="POST">
</form>
Many people get confused about how Rails works - it's really quite simple. Rails employs "helper" methods to generate pure HTML which your browser can read.
Browsers only understand HTML/CSS at the moment. Thus, whenever you send a request to Rails - it has to return that data, otherwise a "web page" simply wouldn't be able to be processed.
Thus, when you ask whether you can nest forms, you have to abide by how the forms work in pure HTML (spec):
Note you are not allowed to nest FORM elements!
HTML fill-out forms can be used for questionaires, hotel reservations,
order forms, data entry and a wide variety of other applications. The
form is specified as part of an HTML document. The user fills in the
form and then submits it. The user agent then sends the form's
contents as designated by the FORM element. Typically, this is to an
HTTP server, but you can also email form contents for asynchronous
processing.
In short, it means that everything within a <form> tag is counted as a form by HTTP. This means that if you have another <form> tag, it's going to cause an error, preventing either from working.
You know this already (otherwise you wouldn't have mentioned ajax).
Your main problem is the use of <form></form> inside your current <form> object. This will confuse HTML profusely, and thus I would recommend replicating the submission of a form, without the <form> object itself:
#app/views/admin/real_estate/form/assessment.html.erb
<%= text_field_tag "[company_name]", params[:company_name], id: real_estate_id %>
<%= button_tag "Submit", type: "button" , data: { id: real_estate_id } %>
#app/assets/javascripts/application.js
$(document).on("click", "button", function(e){
e.preventDefault();
var real_estate_id = $(this).data("id");
$.ajax({
url: "path/to/real/estate/form/assessment/" + $(this).data("id")),
data: {company_name: $("input[type=text]#" + real_estate_id).val()}
success: function(data) {
//do something on success
},
error: function(data) {
//do something on error
}
});
});
This code will still output what you need.
The difference will be two-fold:
The user will experience the same functionality (the input will still be present)
The embedded form will be passed to the main "form" submit, but will not be passed through your strong params method (making it
invisible to your controller)
In effect, you're replicating the functionality of an HTML form through Ajax. Whilst I still wouldn't do it this way, it will give you the functionality you require.

In Rails, how to create a dropdown menu that fills in parameters to a path to navigate to?

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();
});

Resources