Rails capybaraAmbiguous match, found 50 elements matching css - ruby-on-rails

I get this error:
Ambiguous match, found 50 elements matching css "input[value=\"delete\"]"
When I put the following code:
find('input[value="delete"]').first.click
On html file:
<div id="deletediv-38">
<form class="button_to" onsubmit="return confirm('Are you sure you want to delete?')" method="post" action="/del/38">
<input type="hidden" name="_method" value="delete" />
<input type="submit" name="delete-38" value="delete" />
</form>
</div>
<div id="deletediv-39">
<form class="button_to" onsubmit="return confirm('Are you sure you want to delete?')" method="post" action="/del/39">
<input type="hidden" name="_method" value="delete" />
<input type="submit" name="delete-39" value="delete" />
</form>
</div>
.
.
.
find('input[id="delete-38"]') doesn't work , it says element not found.
And I can't count as well.
when I do
find('input[value="delete"]').count.should_be > 0
I again get the error:
Ambiguous match, found 50 elements matching css "input[value=\"delete\"]"

To explain the errors you're having -
find will find one unique element, if there are none or more than one matching you will get an error. Your find('input[id="delete-38"]') fails because according to the html the only elements with ids are divs and have divs of the format deletediv-xx. If you were trying to find the inputs with the name of delete-38 you would do
find('input[name="delete-38"]')
As #dimakura stated to get the first element you use #first. If you want a count of the elements you need to use #all - like so
all('input[value="delete"]').count
If the page is dynamically changing and you want to know the count on the page once at least one has appeared then you could do
all('input[value=delete]', minimum: 1).count
which would use Capybaras waiting behavior for at least one to appear before returning (or it times out while waiting)

Try using this instead:
first('input[value="delete"]').click

Related

I want to POST the content of a uib-timepicker angular-ui bootstrap tag

I would like to let the user pick a time using a uib-timepicker tag and POST the result to my server (with a submit button for example).
<form class="simple_form vertical_form"
data-type="json" id="my_form"
action="url_to_post"
accept-charset="utf8"
data-remote="true"
method="post">
<div class="field">
<uib-timepicker
name="my_time"
ng-model="event.my_model"
hour-step="1"
minute-step="15"
show-meridian="true">
</uib-timepicker>
</div>
<div>
<input type="submit" value="Validate" class="btn btn-success">
</div>
</form>
But when I check to content of the POST method it is empty (I don't get the "my_time" key as I would expect. I guess it is because a "uib-timepicker" does not exactly work as an input field. But I feel like there must be a way to POST the content of that tag.
Thanks for helping!
I solved this issue by adding hidden input elements with binding to the time picker:
<uib-timepicker ng-model="ServiceRestoredTime" name="ServiceRestoredTime" ng-change="changed()" hour-step="hstep" minute-step="mstep" show-meridian="false" show-spinners="false"></uib-timepicker>
<input type="hidden" name="ServiceRestoredTimeParam" value="{{ServiceRestoredTime | date:'HH:mm' }}">

How do I add a hidden input tag using the Jericho HTML Parser?

For each form in an HTML page I want to add an additional hidden input tag as it passes through a JEE Filter. For a given HTML page for example:
<html>
<form name="input" action="submit.jsp" method="post">
<input type="hidden" name="id" value="1"/>
<input type="submit" value="Submit"/>
</form>
</html>
the end result should be similar to this:
<html>
<form name="input" action="submit.jsp" method="post">
<input type="hidden" name="id" value="1"/>
<input type="submit" value="Submit"/>
<input type="hidden" name="dynamickey" value="DYNAMIC_VALUE_HERE"/>
</form>
</html>
As the HTML may be malformed, I am figuring that Jericho would be the HTML parser of choice. After a couple of passes through the web pages, I have found ways to change the values of already existing tags, but how to add an additional tag escapes me.
Thanks in advance for help.

Why does the form not set $_POST['submit'] or pass the filename?

The following code displays
"Array ( [MAX_FILE_SIZE] => 1000000 [caption] => banner )"
when I do a print_r($_POST). I cannot see what I have done wrong.
<?php
$zfilename='File name';
$zcaption='Caption';
?>
<form action="test.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
<p><label for="filename"><?php echo"$zfilename"; ?></label>
<input id="filename"
name="filename"
type="file"
placeholder="file name"
autofocus required/></p>
<p><label for="caption"><?php echo"$zcaption"; ?> </label>
<input id="caption"
name="caption"
type="text"
placeholder="caption"/></p>
<br>
<center>
<button type=submit >Submit</button>
</center>
</form>
I tried "get" as well, same result.
It's because you didn't use any posted data in your PHP.
Do a print_r($_FILES['filename']);
I tried the two answers supplied (one has been removed from the list???) with no luck. Went back to the problem first thing this morning and after checking some more found it was a simple error on my part. I used "type = button" when I should have used "type = submit". I had checked and rechecked every parameter except "type" :(

Do MVC3 non-sequential hidden input indexes need to come first?

MVC3 non-sequential index hidden inputs for model binding..
<input type="hidden" name="Index" value="whatever" />
Does it matter if they go before, after, in the middle of the other related inputs to be posted?
Does it matter at all where they end up in the posted data?
For example, can they all be lumped together and it still works?
<input type="text" name="[A].Id" value="1" />
<input type="text" name="[B].Id" value="2" />
<input type="hidden" name="Index" value="A" />
<input type="hidden" name="Index" value="B" />
No, the order of your form fields does not matter, nore where they appear on the html page.
The most important factor for MVC3 is the name of the fields must match to the name of your controller/action parameter.
If you have two fields with the same name however, only one value will be returned into your action.
As long as the hidden fields are located inside of the form it should not matter the order in which they are placed. Please see code sample below. Notice how the hidden fields are put anywhere inside of the form.
#using (Html.BeginForm())
{
#Html.ValidationSummary(false, "Please correct the following errors")
#Html.HiddenFor(m => m.CoolStuffId)
#Html.Partial("_EditCoolStuff", Model)
<fieldset class="ui-grid-a">
<div class="ui-block-a"><a data-role="button" href="#Url.Action("ActionPlan", "Store", new { id = Model.StoreID })">Cancel</a></div>
<div class="ui-block-b"><button type="submit" data-theme="a">Submit</button></div>
</fieldset>
#Html.HiddenFor(m => m.TypeId)
}

set div as target container for result page of a post method

.I have a div tag that is also divided into two divs. here is the code:
<div>
<div id="search">
<form id="try" method="post">
Batch: <input id="batch" name="batch" type="text"/>Dept: <input id="dept" name="dept" type="text"><input type="submit"/>
</div>
<div id="receiver">
</div>
</div>
I have placed a search option in the div named "search" using post method. what i want to do is that when i click the submit button the page to receive the values will appear on the div named "receiver".
is this possible? if it is, please help..
The ways you have for this are:
1 - The simplest - instead of a div - use an IFrame like this
<div id="search">
<form id="try" method="post" target="receiver" action="url-to-server-Page">
Batch: <input id="batch" name="batch" type="text"/>
Dept: <input id="dept" name="dept" type="text" />
<input type="submit"/>
</form>
</div>
<iframe name="receiver" id="receiver"></iframe>
</div>
The disadvantage in this case is that the search-result that come in this case from url-to-server-page - is a complete and independent HTML page that is nested in your search page.
It is not effected by styles of the parent page, and the communication between them for JavaScript operations is rather combersome.
2 - with some JavaScript skills you can use AJAX.
There are many libraries on the net that can help you do it in very few lines of code.
jQuery is the simplest, although it's the one I like least...
http://jquery.com/
3 - use a full round-trip
<div id="search">
<form id="try" method="post" target="receiver" action="url-to-server-Page">
Batch: <input id="batch" name="batch" type="text"/>
Dept: <input id="dept" name="dept" type="text" />
<input type="submit"/>
</form>
<div id="receiver">
<?php if (isset($_POST['batch']) && isset($_POST['dept'])){
//display search results here.
}
?>
</div>
</div>
You want what's called "AJAX." There are a number of libraries available, such as Google Web Toolkit or jQuery to accomplish what you want.
Can you clarify? Is this a php page and you are posting to self or are you looking to use ajax to populate the div?
If it is php and you post to self then you can simply check if the post has been made inside that div :
<div id="receiver">
<?php if (isset($_POST['batch']) && isset($_POST['dept'])){
//display search results here.
}
?>
</div>

Resources