Rails select with multiple options only sends one - ruby-on-rails

I have this form :
<form accept-charset="UTF-8" action="/contracts/<%= params[:id] %>/addConsultants" method="POST" id="updateConsultants"><!---->
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓">
<input name="authenticity_token" type="hidden" value="<%= session[:_csrf_token] %>">
</div>
<div class="consultant_selector">
<h2>Consultants</h2>
<select name="consultants_available" size="10" multiple>
<% #users.each do |u| %>
<option value="<%= u[:id_user] %>"><%= u[:name] %></option>
<% end %>
</select>
<ul>
<li class="triangle_right" id="add_consultant"></li>
<li class="triangle_left" id="remove_consultant"></li>
</ul>
<select name="consultants_selected" size="10" multiple>
</select>
</div>
<div>
<input type="submit" value="Mettre à jour" />
</div>
</form>
And in my controller :
def add_consultants
#contract = Contract.find(params[:id])
#consultants = params[:consultants_selected];
puts "------ Consultants: #{#consultants}"
redirect_to contract_path
end
The idea is that I can select names from the first select, move them to the second by clicking on the triangle and then submit the form to assign these names to a contract. However, when I do this the call to puts only prints one ID out of the 3 I have in the second select (all three of them have the selected attribute to true). How do I get multiple values?

Just found the answer here : https://stackoverflow.com/a/2196522/1796742
Changing the name of the second select from:
<select name="consultants_selected" size="10" multiple>
</select>
To :
<select name="consultants_selected[]" size="10" multiple>
</select>
Worked.

Related

Select2 and Thymeleaf integration

I created a simple search bar with multiple options enabled by the Select2 library:
<form th:action="#{/searchuser}" method="post">
<div th:object="${searchInput}">
<div class="input-group mb-3">
<input type="text" th:field="*{key}" class="form-control form-control-lg" placeholder="Search Here">
<div class="input-group-prepend">
<span class="input-group-text" type="submit"><i class="fas fa-search"></i></span>
</div>
</div>
<select class="select2-multiple-choices select2-choice" multiple="multiple" th:field="*{parameters}">
<option th:value="'id'" value="id"> Id</option>
<option th:value="'First Name'" value="First Name"> First
Name</option>
<option th:value="'Middle Name'"> Middle Name</option>
<option th:value="'Last Name'" value="Last Name"> Last Name</option>
<option th:value="'mail'" value="Mail"> E-Mail </option>
</select>
</div>
</div>
The controller:
#RequestMapping(value = "/searchuser", method = RequestMethod.POST)
public ModelAndView searchUser(Locale locale,
#ModelAttribute("searchInput") SearchUserDTO searchdto,
BindingResult searchBindResult,
Model model) {....}
SearchUserDTO contains String key and List parameters.
I would sent to server the search key and the selected options. How can I get the selection and pass them with thymeleaf?

parameter not being submitted

The following is an excerpt of a rails form
<%= form_with(model: #usercontent) do |form| %>
where the following html renders a collection
<div class='alert'>
<select class='image-picker show-html' name="ki_id" id="ki_id">
<div class='row'>
<div class='buttonselector'>
<label for="usercontent_ki_id_1">
<input class="invisi-selector" type="radio" value="1" name="usercontent[ki_id]" id="usercontent_ki_id_1" />
<img src='/assets/ki/circle-15-1c2a221aa37a24e5b23acd7124cd47ae2a17434af589353a063633a6ef5abca6.svg'>
<div></div>
</label>
</div>
</div>
<div class='row'>
<div class='buttonselector'>
<label for="usercontent_ki_id_2">
<input class="invisi-selector" type="radio" value="0" name="usercontent[ki_id]" id="usercontent_ki_id_2" />
<img src='/assets/ki/aerialway-15-621960ddf5e9930f1b1102faa4ee23542e11163f48a67470f1fe245abbc27955.svg'>
<div></div>
</label>
</div>
</div>
[...]
</select>
</div>
However, when the user selects an item, the parameter being submitted is "ki_id"=>"0". Where is the above structure wrong?
It is submitted due to name="ki_id" in
<select class='image-picker show-html' name="ki_id" id="ki_id">
Can you show the full code for the view.

HTML - "Search by" and how to filter by selected field

My task is to add a combo-box in the search page to allow a user to select the field she wants to search by and then search for customers using that field. In other words, there should be a “Search By” combo-box on the index page that the user selects the field to search by (Last Name, First Name, etc.) and then types in a search string and clicks a find button to perform a search and display the results on the same page. Search results should be sorted based on the field used for the search.
Currently, I have the following code:
<form asp-action="Index" method="get">
<div Class="form-actions no-color"></div>
<div class="search-item">Search</div>
<div class="sigform_fld search-item">
<select class="catalog">
<option value="1">Customers</option>
</select>
</div>
<div class="search-item">By</div>
<div class="sigform_fld search-item option">
<select class="option-1">
<option value="2">Last Name</option>
<option value="3">Email Address</option>
<option value="4">Company Name</option>
</select>
</div>
</form>
<form asp-action="Index" method="get">
<div Class="form-actions no-color">
<p>
<input type="text" name="LastName" value="#ViewData("currentFilter")" />
<input type="submit" value="Search" class="btn btn-default" />
| #Html.ActionLink("All Customers", "Index")
</p>
</div>
</form>
This only filters by Last Name. How can I have one search bar that filters by the selected option (Last Name, Company Name or Email address).
Help!

Rails mechanize gem - scraping

I want to fill in some options which trigger the downloading of a file, using the Mechanize gem.
The HTML is the following.
<div>
<form action="" id="label_selectors" method="post">
...
...
<div class="source form_options">
<select name="source" id="source">
<option value="btce" selected="selected">Btc-e</option>
<option value="cryptsy">Cryptsy</option>
<option value="796">796</option>
<option value="bitstamp">Bitstamp</option>
<option value="formulas">Altcoin Indexes</option>
</select>
</div>
<div class="label form_options">
<select name="label" id="label">
</select>
</div>
<div class="period form_options">
<select name="period" id="period">
<option value="15m">15 minute</option>
<option value="1h" selected="selected">1 hour</option>
<option value="1d">1 day</option>
</select>
</div>
<div class="presense form_options">
CSV: <input type="radio" name="presence" value="csv"> Chart: <input type="radio" name="presence" value="chart" checked="checked">
</div>
<div class="submit form_options">
<input type="submit" name="submit" value="OK">
</div>
</form>
</div>
I tried to do something like this:
form = page.forms.first
form.source = "btce"
form.label = "BTC/USD"
form.period = "1d"
form.presense = "csv"
form.submit
However, it doesn't work:
NoMethodError: undefined method `presense=' for #<Mechanize::Form:0x007fb878dc7f98
(the typo presense rather than presence appears in the page)
How could I fill in this form correctly?
It looks like the presence input is a radio button.
To select a radio button with mechanize you'll want something like:
form.radiobuttons_with(name: 'presence')[0].check
There's other ways to select radio buttons with mechanize - you can see them here: http://docs.seattlerb.org/mechanize/GUIDE_rdoc.html

ASP.Net MVC 4 C# login form with PhantomJS and Selenium

I have a specific question, as i can't figure out how to submit form without "id" or "name" in selenium.webdriver (phantomjs) there is html logim form:
<div class="ui-helper-child-2 ui-page-column-last ui-page-column-span-12">
<div class="ui-page-column-block ui-widget-signin">
<h1>Sign In</h1>
<form action="/glo/en-us/session,create/view.html" method="post">
<div class="ui-helper-child-1">
<label class="ui-state-required" for="username">Customer ID:</label>
<input id="username" name="username" maxlength="9" type="text" value="" />
</div>
<div class="ui-helper-child-2">
<label class="ui-state-required" for="password">Password:</label>
<input id="password" name="password" maxlength="20" type="password" value="" />
</div>
<div class="ui-helper-child-3">
<label class="ui-state-required" for="companyId">Company:</label>
<select id="companyId" name="companyId">
<option selected="selected" value="glo">Choose company</option>
<option value="asp">new to asp</option>
<option value="aeg">who is it</option>
<option value="asus">who use this brand name</option>
</select>
</div>
<div class="ui-helper-child-4">
<input id="callback" name="callback" type="hidden" value="/glo/en-us/home,show/view.html?error=1" />
<input type="submit" value="Sign In" />
</div>
</form>
</div>
and one more time how to login? I can add information to by webdriver.FindElement(username) and so on, but not know how to post data as i don't have id or name in submit element.
I think this will work for you:
driver.FindElement(By.CssSelector(".ui-helper-child-4>[type='submit']")).Click();

Resources