Rails - Submit Values based on Multiple Selection and Text Field - ruby-on-rails

I'm still new in Rails. I'm trying to have a page where a user can select multiple items from a list then add some description into a text field. And at the end, the user should submit this information. How can I retrieve submitted data?
Any help would be greatly appreciated.
Thanks!
Here's the code for now:
<select multiple >
<option>Table</option>
<option>Door</option>
</select>
<br><br><br>
<select multiple >
<option>Blue</option>
<option>Red</option>
</select>
<input type="text" id="post_user" name="post" size="20" value="" />

All the form (POST) data can be found in the params hash. See attached links for details:
http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters
Rails params explained?

Related

How to submit multiple values from a select2 dropdown in html form

Context: This contains 1 select2 dropdown with the ability to select multiple options
<div class="form-group" id="mediaBox" >
<label for="i">
Media Type
</label>
<select class="form-control" name="mediatype" style="width: 100%;" multiple="multiple" id="i">
<option>Original</option>
<option>Videogame</option>
<option>Comic and/or Manga</option>
<option>TV</option>
<option>Movie</option>
<option>Novel and Book</option>
</select>
</div>
Pictures of problem(Visual representation of the problem):
Picture 1(Second select2 dropdown):
Output:
Problem:
When I submit a form with multiple values from a select2 dropdown(I have 1 select2 dropdowns in this form that allows me to select multiple values) they each submit only one value I selected and not all of them for the mediatype variable. Can you help me solve this problem? Modify any html code if necessary.

How to create a dropdown with 'option group' on Grails 4?

I wanna create a dropdown with option group. Controller return a list which is options items and get the list on GSP page. I need to populated that option item with option group.
<select class="" name="name" id="name">
<g:each in="${list}" var="opt">
</g:each>
</select>
Anybody can give an example with backend code. Thanks
You should use the <g:select ...> tag.
<g:select optionKey="id" optionValue="title"
name="book.title" from="${bookList}" />
See https://gsp.grails.org/latest/ref/Tags/select.html.

Is it good or bad practice to rely on name attribute in the form to post the model to MVC controller?

I have a model containing some complex types including nested lists. So I cannot simply use the model.
My initial thought is using knockoutjs to take care of the user interactions (add/delete list items etc.), and post knockoutjs view model via ajax.
But after some googling, I found I could give meaningful name attributes in my form elements and do regular form submit to post my form model like this.
<input checked="checked" data-val="true" data-val-required="The Boolean field is required." name="Questions[0].SubQuestions[0].OptionAnswers[0]" type="checkbox" value="true">
Of course I still need some javascript for list manipulations and stuff.
Which approach is more preferred?
EDIT:
Sorry for the indexing issue. I was aware it should be sequential and start from 0. I just took a random element from my generated html. The indexing is not really my issue. I just would like to know which approach is more preferred.
I start liking the regular form submission way as it takes advantage of the validation feature of DataAnnotations and Html.ValidationSummary. What's the advantages for ajax post?
You are very close to understand the concept how asp.net mvc model binder works out of the box.
This is the link to an article from where I learnt how to achieve similar requirement.
The way that mvc model binder reads in the properties is by looking for parameterName[index].PropertyName. The index must be zero-based and unbroken. Say suppose, if you have your html like below which is a list for example and it can be your Questions list which is a complex type:
<input type="text" name="people[0].FirstName" value="George" />
<input type="text" name="people[0].LastName" value="Washington" />
<input type="text" name="people[1].FirstName" value="Abraham" />
<input type="text" name="people[1].LastName" value="Lincoln" />
<input type="text" name="people[3].FirstName" value="Thomas" />
<input type="text" name="people[3].LastName" value="Jefferson" />
When you submit the above values to asp.net mvc controller, the above value turns into:
people%5B0%5D.FirstName=George&people%5B0%5D.LastName=Washington&people%5B1%5D.FirstName=Abraham&people%5B1%5D.LastName=Lincoln&people%5B3%5D.FirstName=Thomas&people%5B3%5D.LastName=Jefferson
The mvc model binder then read the above data as:
people[0].FirstName = "George"
people[0].LastName = "Washington"
people[1].FirstName = "Abraham"
people[1].LastName = "Lincoln"
people[3].FirstName = "Thomas"
people[3].LastName = "Jefferson"
So, as you can see we have list items for index 0, 1 and then 3. It is broken! This results in mvc model binder only translating this data to a list of type people for only two items i.e. index 0 and 1. It excludes 3 as there was no data at index 2.
this answer has information from the same article. If you understand this, you will understand the role of name attributes and you will appreciate how easy it is to follow the rules and mvc binder will understand all your posted data.
If you want to make sure that mvc binder constructs a list from the posted data even if any index is missing then refer Eric's answer
Yoda's answer is very very good. However there is a known way to have a list of Non-Sequential Indices for postback as Phil Haacked describes in Model Binding To A List (Non-Sequential Indices heading)
<input type="hidden" name="products.Index" value="0" />
<input type="text" name="products[0].Name" value="Beer" />
<input type="text" name="products[0].Price" value="7.32" />
<input type="hidden" name="products.Index" value="1" />
<input type="text" name="products[1].Name" value="Chips" />
<input type="text" name="products[1].Price" value="2.23" />
<input type="hidden" name="products.Index" value="3" />
<input type="text" name="products[3].Name" value="Salsa" />
<input type="text" name="products[3].Price" value="1.23" />

ExpressionEngine, Safecracker form embed select box value in URL in safecracker form

I have an embed in my safecracker form.
Embed has a select box and can select a name of
a Tea.
I want to send name of the Tea with safecracker URL to next page.
This is the code:
{exp:safecracker channel="blending_log" return="sthome/blending/ENTRY_ID" dynamic_title=" [selected_tea]_[organic_or_conventional][date_time][agent_number]"}
<h3>Select and enter data</h3>
{embed="stembeds/.blending_select_tea"}
{!--------Receive data to create value--------}
<input type="hidden" name="title" value="" />
<br /><br />
Organic Or Conventional:
{field:org_con}
Agent Number:
{field:agent_number}
{!-----END Receive data to create lot number-------}
The embed is:
{embed="stembeds/.blending_select_tea"}
This is the code in embed:
<select name="selected_tea">
<option selected="selected" >Select a Tea</option>
{exp:channel:entries channel="tea_herbs" order_by="title" sort="asc"}
<option name={title} id={entry_id} value={url_title}>{title}</option>
{/exp:channel:entries}
</select>
I want to select tea from embed and I want to send the Tea name with safecracker URL:
{exp:safecracker channel="blending_log" dynamic_title="[selected_tea]_[organic_or_conventional][date_time][agent_number]" return="sthome/blending2_nimmi/ENTRY_ID"}
Please help me to solve this issue.
Thank you very much
Make sure you have a text field in your "blending_log" channel with the name "selected_tea" - otherwise SafeCracker won't save that value.
Your embed should look like this:
<select name="selected_tea">
<option value="" selected="selected">Select a Tea</option>
{exp:channel:entries channel="tea_herbs" order_by="title" sort="asc" dynamic="no" disable="member_data|pagination|categories"}
<option value="{url_title}">{title}</option>
{/exp:channel:entries}
</select>

select checkbox with hidden input type

Folks,
I am using watir-webdriver, I have a piece in my HTML DOM which gets generated on the fly when I enter some credentials, this piece has a bunch of checkboxes, the number of checkboxes vary, I have to select one checkbox, below is an example of this, here I want to select the second checkbox(the one that has value "BS" for the input type hidden but the value for input type checkbox is same for all):
<li class="dir">
<input type="checkbox" value="1" onclick="$(this).next('.should_destroy').value = (this.checked?0:1)" name="should_not_destroy">
<input class="should_destroy" type="hidden" value="1" name="import[dir_attributes][][should_destroy]">
<input type="hidden" value="" name="import[dir_attributes][][id]">
<input type="hidden" value="Automation" name="import[dir_attributes][][path]">
<span class="dir_mode">Include</span>
Automation
</li>
<li class="dir">
<input type="checkbox" value="1" onclick="$(this).next('.should_destroy').value = (this.checked?0:1)" name="should_not_destroy">
<input class="should_destroy" type="hidden" value="1" name="import[dir_attributes][][should_destroy]">
<input type="hidden" value="" name="import[dir_attributes][][id]">
<input type="hidden" value="BS" name="import[dir_attributes][][path]">
<span class="dir_mode">Include</span>
BS
</li>
I may be able to do this with XPATH, but wanted to try a non XPATH solution. The input type hidden has the appropriate value that I need, for example above the second checkbox has value "BS" for input type hidden. I tried to use the hidden method like this:
h = ##browser.hidden(:value, "BS")
h.select
But I dont know what to do after this. I am trying to select the checkbox based on the value of the hidden element. Any feedback is much appreciated.
I would suggest using the visible elements instead. I think it makes it easier to read the test and seems more stable.
Try:
##browser.li(:class => 'dir', :text => /BS/).checkbox.set
Here we go, I think this will do it
Since you have to select the checkbox based on the hidden, you're going to have to go up a level to the containing li, then drill down to the checkbox
#browser.hidden(value: 'BS').parent.checkboxes.first.set

Resources