want to parse href in ruby on rails using nokogiri - ruby-on-rails

I am using nokogiri as my HTML parser.
<html>
<body>
<form>
<table>
<tr><td>Some Text</td></tr>
<tr>
<td colspan="2" align="center">
<br />
<a href="TransportRoom?servlet=CaseSearch.jsp&advancedSearch=Advanced">
Advanced Search
</a>
<br />
</td>
</tr>
</table>
</form>
</body>
</html>
In this html code I want to parse the "Advance Search" link. This html is saved in variable named doc1
Can anyone help me with this?

Should be as simple as
doc = Nokogiri::HTML(doc1)
href = doc.css("a").first.attr('href')
This is what you want?

First answer is working for me but if there is n number of links than we can manipulate it by this way
html = Nokogiri::HTML(doc1)
html.css("a").each do |element|
if (element.text.strip == 'Advanced Search')
advance_search_link = element.attr('href')
end
end

I would do as below :
require 'nokogiri'
#doc = Nokogiri.HTML <<-eotl
<html>
<body>
<form>
<table>
<tr><td>Some Text</td></tr>
<tr>
<td colspan="2" align="center">
<br />
<a href="TransportRoom?servlet=CaseSearch.jsp&advancedSearch=Advanced">
Advanced Search
</a>
<br />
</td>
</tr>
</table>
</form>
</body>
</html>
eotl
#doc.at_xpath("//a[normalize-space(.)='Advanced Search']")['href']
# => "TransportRoom?servlet=CaseSearch.jsp&advancedSearch=Advanced"

Related

The page is validated automatically after I set a validator and every time I want to redirect to another page, I get the error from validator?

I though I have finished working on the page, however, after another round of checking, it seems that the previous working link is not working anymore and every time when clicked, tries to validate the page.
The link is a part of a main master page.
Looks like the Post Back event called from the clicking on the link is trying to validate Date Pickers again.
I have the following logic to build CustomValidator and RadGrid, where I validates RadDatePickers:
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">
<div align="center">
<asp:CustomValidator ID="checkForTwoDates" ClientValidationFunction="AtLeastOneDate_ClientValidate" ViewStateMode="Enabled"
EnableClientScript="True" ErrorMessage="At least one date should be selected" runat="server"></asp:CustomValidator>
<table class="moss2Search">
<tr>
<td>
<div runat="server">
<telerik:RadDatePicker RenderMode="Lightweight" ID="RadDatePicker1" width="100%" runat="server" DateInput-Label="Boarding Start Date"></telerik:RadDatePicker>
</div>
</td>
<td>
<div runat="server">
<telerik:RadDatePicker RenderMode="Lightweight" ID="RadDatePicker2" width="100%" runat="server" DateInput-Label="Boarding End Date">
</telerik:RadDatePicker>
</div>
</td>
<td>
<div>
<asp:CheckBox ID="chkMerActive" runat="server" Checked="true"/>Active
</div>
</td>
<td>
<telerik:RadButton RenderMode="Lightweight" runat="server" Text="Search" ID="Button1" OnClick="btnSearch_Click"></telerik:RadButton>
</td>
</tr>
</table>
</div>
</telerik:RadAjaxPanel>
<asp:Label ID="lblMsg" ForeColor="red" runat="server"></asp:Label>
<br />
<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="ajaxControl">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="ajaxUpdateControl"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<div>
<telerik:RadGrid RenderMode="Lightweight" runat="server" ID="grdMoss2Merchants" AllowPaging="True" AllowSorting="true" PagerStyle-AlwaysVisible="true" OnNeedDataSource="BindToDatasource">
<ExportSettings IgnorePaging="true">
<Excel WorksheetName="Moss2Merchants" />
</ExportSettings>
</telerik:RadGrid>
</div>
What am I doing wrong or missing?

Jenkins pipeline script to send customized email

I've been trying to use my customized content for the Jenkins build emails. I got stuck when I am trying to change the contents of html dynamically based on the results from previous stages. I am passing the contents of an html to a variable as below and when i try to access the hash list in that, the value of arrays(key and value of an array) are not getting printed.
def content = """\
<html>
<head>
<style>
</style>
</head>
<body>
<table style=" background-color:lightgreen; width:900px;margin:0;">
<tr>
<th>Build Results :</th>
</tr>
<tr>
<td>project Name : </td>
<td>project URL : </td>
</tr>
<tr>
<td>Build Number : ${var3} </td>
</tr>
<tr>
<td>Buid URL :</td>
</tr>
</table>
<table style=" background-color:lightblue; width:900px;margin:0;">
<tr>
<% stagearray.each { item ->
def key=item.key;
def value=item.value; %>
<td>${key}</td>
<td>${value}</td>
<% } %>
</tr>
this is the sample
<tr><td>This is test line </td></tr>
</table>
</body>
</html>
"""
Any help would be highly appreciated.

How to disable label of <s:textfield> tag in Struts2

I have the following tag in struts2:
<s:textfield type="text" key="maquina" label="" labelSeparator="" style="width:100;"/>
and I want to disable the label by this tag, is this moment in my html code is generated as shown below:
<table>
<thead>
<tr>
<th>Hora</th>
<th>Ruta</th>
<th>Maquina</th>
</tr>
</thead>
<tbody>
<%int i=10;%>
<s:iterator value="datosPlan" var="datosPlanLoading" status="valorDatosPlan">
<%--//cambia de color la fila en la que se encuentra le cursor--%>
<tr id="<%=i%>"
<%
if(i%2 == 0)
{
%>
class="alt"
style="background: #E1EEf4;color: #00557F;"
onmouseover="destacarFila(this.id);"
onmouseout="colorOriginalFila(this.id,0);"
<%
}
else
{ %>
onmouseover="destacarFila(this.id);"
onmouseout="colorOriginalFila(this.id,1);"
<%
}
i++;
%>
>
<%--<input type="hidden" id="<%="editar"%>${listaHorariosLoading.codigoHorario}" value="${listaHorariosLoading.nombreHorario}">--%>
<td>
<p>
<s:property value="hora"/>
</p>
</td>
<td>
<p>
<s:property value="ruta"/>
</p>
</td>
<td>
<p>
<s:textfield type="text" name="maquina" style="width:100;"/>
</p>
</td>
</tr>
</s:iterator>
</tbody>
therefore I want to disable label because I want to just the input type.
thanks in advance.
If you use key="maquina" then it will generate label for you.
If you use label="" then it will generate label for you.
Don't use key and also do not use label attributes.
Try this
<s:textfield type="text" id="maquina" name="maquina" style="width:20;" />
Output:
<input id="maquina" type="text" style="width:20;" value="" name="maquina">
</input>
EDIT :
<td class="tdLabel"></td> will be generated if you use <s:form>
Try to use html form tag <form> instead of <s:form>.
For Example
<form action="someAction" method="get" >
<s:textfield type="text" id="maquina" name="maquina" style="width:20;" />
</form>
Note:
And if you want to use tag then you need to implement either template in freemarker or custom theme.
Tutorial 1
Tutorial 2
In struts.xml
<constant name="struts.ui.theme" value="simple"/>
will also remove default template.
because I want to just the input type
This is only possible if you use simple theme configured to be used by the tag.

Need to format table in MVC view to generate PDF

I'm trying to create a pdf and this is my view that generates that PDF, however for some reason the styling and formatting isn't working. I have tried to align text and add css styling but nothing seems to be working. Does anyone have some advice on how I can get the table to display the top row to be one line of information instead of getting cut off and moved under. Here is what the PDF curently looks like; as you can see the row doesn't look right. I need that row to be one single line flowing together. Any advice would be greatly appreciated.
Here is my MVC view that is generating the PDF:
#model List<ArdentMC.DHS.COP.Mobile.MVC.Models.Reports>
#{
Layout = "";
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div style="color:red; align:center;">UNCLASSIFIED/FOR OFFICAL USE ONLY</div>
<table style="width:300px;">
#foreach (var item in Model)
{
<tr>
<td> #item.Phase </td>
<td> #item.NocNumber</td>
<td> #item.Title</td>
<td> #item.Location</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>#item.ReportDateText</td>
</tr>
}
</table>
#foreach (var item in Model){
#Html.Raw(item.Description)
}
<br /><br /><br /><br /><br />
<br /><br /><br /><br /><br />
<div style="color:black; align:center;">US Department of Homeland Security</div>
<br />
<div style="color:red; align:center;">UNCLASSIFIED/FOR OFFICAL USE ONLY</div>
</body>
</html>

reading from a file using the input type=file in grails

Hi I have the following code in my grails gsp
<form action="upload-script-url" method="post" enctype="multipart/form-data">
<table class="table"style="width: 75%">
<tr>
<td>
<span style="font-weight: bold; ">Select the Source File:</span>
<input size="75" type="file" id="payload" name="payload"/>
</td>
</tr>
<tr>
<td>
<input type="submit" class="red" id="Run">Run</button>
</td>
</tr>
</table>
</form>
I read the form parameters from: here
Are those right parameters in the html form?
Now how should I proceed to read the data from the selected file? do I have to use the apache commons fileupload api ?
Thanks
request.getFile("payload")
and you will get a CommonsMultipartFile
If you take some time to (again) actually look at the documentation you will see how to do it...

Resources