why adding a simple div to HAML will break the code? - haml

it is quite strange that the following works:
- 1.upto(30) do |i|
= i
%br
but the following won't:
%div this line
- 1.upto(30) do |i|
= i
%br
Isn't the second part just to add a div and let the numbers go into that div?

Was the this line part intended? Or are you indicating the error position? Because without it, the snippit works.
If this line is supposed to be content, then what you want is probably:
%div
this line
- 1.upto(30) do |i|
= i
%br

Related

beautifulsoup: get text (including html tags) between two different tags (</h3> and <h2>)

I am trying to scrape an html file structured as follow using beautifulsoup. Basicaly, each unit is constisted of:
one <h2></h2>
one <h3></h3>
more than one <p></p>
Something like follow:
<h2>January, 2020</h2>
<h3>facility</h3>
<p>text1-1</p>
<p>text1-2</p>
<h2>April, 2020</h2>
<h3>scientists</h3>
<p>text2-1</p>
<p>text2-2</p>
<h2>June, 2020</h2>
<h3>lawyers</h3>
<p>text3-1</p>
<h2>.....
I want to get text including the <p> tags between </h3> and the next <h2>. The result should be:
for row #1:
<p>text1-1</p>
<p>text1-2</p>
for row #2:
<p>text2-1</p>
<p>text2-2</p>
for row #3:
<p>text3-1</p>
Here is what I tried so far:
num_h2 = len(soup.find_all('h2'))
for i in range(0,num_h2):
print('---------')
print(i)
p_string = ''
sibling = soup.find_all('h3')[i].find_next_sibling('p').getText()
if sibling:
p_string += sibling
else:
break
print(p_string)
The problem with this solution is that it only shows the content of the first <p> under each unit. I do not know how to find how many <p> are there to generate a for loop. Also, is there a better way to do this than using find_next_silibing()?
Maybe css selectors can help:
for s in soup.select('h3'):
for ns in (s.fetchNextSiblings()):
if ns.name == "h2":
break
else:
if ns.name == "p":
print(ns)
Output:
<p>text1-1</p>
<p>text1-2</p>
<p>text2-1</p>
<p>text2-2</p>
<p>text3-1</p>

RSpec: Cant Verify there are more than 5 Btns on Search Result Page

I try to do an Selenium-Acceptancetest 'Search Result List' with RSpec spec
And my Issue is
It should verify the count of a Button "Detail"
I would be happy to get help for start thinking as a coder; i am a manual tester stil.
My Problem now is:
Method Error
Failure/Error: expect(#driver.find_element(:xpath, "//a[contains(text(),'Details')]").to be > 4)
NoMethodError:
undefined method `to' for #<Selenium::WebDriver::Element:0x00000003fd4938>
Sources:
On my way to resolve the issue I tried to modify this and I found this
But its not working
thanksfully remaining!
my Test File:
# coding: utf-8
puts "this is #{File.basename(__FILE__)}"
extend RSpec::Expectations
extend RSpec::Matchers
describe 'SEL' do
before(:each) do
#driver = loadDriver()
end
after(:each) do
#driver.quit
end
it 'test_Page (SEL)' do
#get the page
...
#do input keyword 'Restaurant'
...
#click submit
...
#(Works!) temp Validation1: Is there a "Btn Details" in SearchResultList?
expect(#driver.find_element(:xpath, "//a[contains(text(),'Details')]").displayed?)
#(Works not!) Validation: Are there more than "5 Btns Detail"
expect(#driver.find_element(:xpath, "//a[contains(text(),'Details')]").to be > 5)
end
end
Update after first answer:
Given I use
expect(#driver.find_elements(:xpath, "//a[contains(text(),'Details')]")).to be > 0
it hits that error:
Failure/Error: expect(#driver.find_elements(:xpath, "//a[contains(text(),'Details')]")).to be > 0
expected: > 0
got: [#<Selenium::WebDriver::Element:0xa6219008 id="a2a2c83a-e52d-4464-81ec-4fce07ccc0b6">, #<Selenium::We...64378279">, #<Selenium::WebDriver::Element:0x..f90cc8d0e id="de990e21-eecf-48db-873b-76515dba7c3e">]
Given the error message:
undefined method `to' for #<Selenium::WebDriver::Element:0x00000003fd4938>
I suspect your issue is a missing bracket. Normal Rspec syntax is
expect(something).to eq(some value).
I would imagine you are missing the closing parenthesis
expect(#driver.find_element(:xpath, "//a[contains(text(),'Details')]").to be > 5)
and this should be something more like:
expect(#driver.find_element(:xpath, "//a[contains(text(),'Details')]")).to be > 5
Although I might guess you need to call a size function on the latter part as well as maybe use find elements. Maybe this will also be useful for you.

Will Paginate and Squeel not working well together

I'm having trouble making will_paginate work together with squeel.
The following code makes a search using params from a form.
def self.search(params)
if params
campo_a_pesquisar = params[:column]
item_status_fisico = params[:item_status_fisico]
item_status_logico = params[:item_status_logico]
criterio = params[:criterio]
q = self
q = q.where{item_status_logico_id =~ my{item_status_logico}} unless item_status_logico.nil?
q = q.where{item_status_fisico_id =~ my{item_status_fisico}} unless item_status_fisico.nil?
q = q.where{(__send__(column) =~ my{criterio})} unless criterio.empty?
q = q.where{(id != 0)} if criterio.empty?
else
self
end
end
It works for "Disponível" (item_status_logico) and "Funciona" (item_status_fisico). But if I put something on :criterio it breaks with the following message:
undefined method `paginate' for nil:NilClass
My controller has a list method which invoke everything
def list
#itens = Item.search(params[:item]).paginate(:page => params[:page])
end
I look on the following material to figure out. But I couldn't.
https:// github.com/ernie/squeel/wiki/Tips-and-tricks
https:// github.com/ernie/squeel
http:// railscasts.com/episodes/354-squeel
Can anyone help?
Info:
ruby (1.9.3)
will_paginate (3.0.4)
squeel (1.0.18)
rails (3.2.13)

Local Jump Error No block given (yield) error on find_each

I'm trying to step through a list of records that are retrieved with find_each.
I patterned my controller code on the answer in this stack overflow post, but I'm still getting a "No Block Given (Yield)" error.
I'm just getting started in Ruby and Rails and I haven't yet found a full fledged explanation (lots of basic examples though) of blocks and yield that gives me what I need.
My code looks like this:
def select_save
#class = params[:class]
#student_id = params[:id]
#class.each do |id|
old_subject = Subject.find(id)
new_subject = old_subject.dup
new_subject.student_id = #student_id
new_subject.save
Assignment.find_each.where(:subject_id => id) do |assignments|
assignments.each do |a|
new_assignment = a.dup
new_assignment.subject_id = new_subject.id
new_assignment.save
end
end
end
respond_to do |format|
format.html { redirect_to #student, :notice => 'Subject and assignments created.' }
end
end
and the error points to the line with find_each.
I know I need a block to yield to, but how exactly that would look in this particular case escapes me.
Thanks for any suggestions.
You're passing a block to where and no block to find_each. You can't do that. You need to reverse find_each and where on this line, the order is important as the block is passed to the last method invoked:
Assignment.find_each.where(:subject_id => id) do |assignments|
It should read:
Assignment.where(:subject_id => id).find_each do |assignments|
Your next problem is you're trying to iterate over assignments, which is a single Assignment. find_each is already doing the iteration for you, passing one assignment into the block at a time. The block should read:
Assignment.where(:subject_id => id).find_each do |assignment|
new_assignment = assignment.dup
new_assignment.subject_id = new_subject.id
new_assignment.save
end
I'm going to make the assumption that your Subject has many Assignments, since you have a subject_id inside your Assignment class. If this is the case, the final and most correct way to write your loop would be:
old_subject.assignments.each do |assignment|
new_assignment = assignment.dup
new_assignment.subject_id = new_subject.id
new_assignment.save
end

Error while inserting image in table using Prawn

I am working with Prawn to generate a pdf, I have to insert an image in a cell of a table.
My code is like this:
image = "path to file"
subject = [["FORMATIVE I "," SUMATIVE ","GRAPH"],
[formative_1,sumative, {:image => image}]
]
table subject
But, I get an error which says:
prawn/table/cell.rb:127:in `make': Content type not recognized: nil (ArgumentError)
How can I resolve this? Any help is greatly appreciated.
Cheers!
In the current version of Prawn 0.12.0 it is not possible to embed images in a Prawn::Table, but this feature seems to be under way, see here. At the moment you have to write your own table, something like
data = [[{:image => "red.png"},{:text => "Red"}],
[{:image => "blue.png"},{:text => "Blue"}]]
data.each_with_index do |row,i|
row.each_with_index do |cell,j|
bounding_box [x_pos,y_pos], :width => cell_width, :height => cell_height do
image(cell[:image], ...) if cell[:image].present?
text_box(cell[:text], ...) if cell[:text].present?
end
x_pos = (x_pos + cell_width)
end
y_pos = (y_pos - cell_height)
end
Prawn in version 0.12.0 doesn't give the possibility to insert image in a cell. Look at this for further information. It works on the next version 1.0.0.rc1. Just change your version. You can also use the tricky way, but I advise you not to do so.
The manual is available here.
The commit and explanation for that feature from the author. Here