ruby - if string is not contained within an array - ruby

I only want to output one anchor here. If the current_page is in the array I get two (.html and -nf.html). If it is not in the array I get as many anchors as there are items in the array.
I am using StaticMatic.
- if page_options[:current_index] < page_options[:total_index] && page_options[:current_index] > 0
// loop through the pre-flash array and if current page matches, add -nf
- page_options[:pre_flash] = ['string_one-nf', 'string_two-nf', 'string_three-nf']
- page_options[:pre_flash].each do |each_string|
- if current_page.include? each_string
%li
%a{ :href => "page#{page_options[:current_index]}-nf.html", :class => "next" }
Next
- else
%li
%a{ :href => "page#{page_options[:current_index]}.html", :class => "next" }
Next

unless current_page.include? the_string
Edit:
You could break the each-loop if you want your first finding to be the only one.
But now this looks a little bit weird, because you are iterating over an array
and breaking after the first element no matter what happens.
Am I addressing your problem at all?
options[:pre_flash] = ['string_one-nf', 'string_two-nf', 'string_three-nf']
page_options[:pre_flash].each do |each_string|
if current_page.include? each_string
%li
%a{ :href => "page#{page_options[:current_index]}-nf.html", :class => "next" }
# This is the last ancor
break
else
%li
%a{ :href => "page#{page_options[:current_index]}.html", :class => "next" }
# This is the last ancor
break
end
end

Okay, so I think we're checking that none of page_options[:current_index] are substrings of current_page.
if page_options[:current_index] < page_options[:total_index] && page_options[:current_index] > 0
found_item = false
// loop through the pre-flash array and if current page matches, add -nf
- page_options[:pre_flash] = ['string_one-nf', 'string_two-nf', 'string_three-nf']
- page_options[:pre_flash].each do |each_string|
- if current_page.include? each_string
found_item = true
%li
%a{ :href => "page#{page_options[:current_index]}-nf.html", :class => "next" }
Next
# do whatever you need to get out of the staticmatic block...
- if !found_item
%li
%a{ :href => "page#{page_options[:current_index]}.html", :class => "next" }
Sorry - I misunderstood what you were doing... thought you were doing an include? on an array but it was a string... :-)

Related

Selenium::WebDriver::Error::ElementNotVisibleError while looping to click button using Watir

I am trying to click a button on loop basis.
Let us say I have following code
if ind == 2
export_id = #browser.div(:id => pop_id).div(:class => /actionDropDownItem groupChild nonSelectable/,:title => "Export").id
#browser.div(:id => export_id).click
else
#browser.div(:id => pop_id).div(:class => /actionDropDownItem groupChild nonSelectable/,:title => "Export").click
end
But it shows error at index value "2" and the Error is
Selenium::WebDriver::Error::ElementNotVisibleError:
Element is not currently visible and so may not be interacted with
Can anyone help me in this case?
It would be great if you provide proper html .
Try this once , this would work for you
if ind == 2
export_id = #browser.div(:id => export_id)
export_id.click
else
#browser.div(:id => pop_id).div(:class => /actionDropDownItem groupChild nonSelectable/,:title => "Export").click
end

comparison of Fixnum with Array failed (ArgumentError)

When the script wants to increment progressbar value it gives me:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/ruby-progressbar-1.4.0/lib/ruby-progressbar/components
/progressable.rb:93:in `<=': comparison of Fixnum with Array failed (ArgumentError)
The Script is shown belove. I ftryed to find solution but no luck. Can Anyone help me with this?
korpusu_id = []
container.divs(:class => "resItem")
.find_all { |div| div.span(:class => "theText", :text => /TestAuto/).exists? }
.each do |korpuss|
id = korpuss.span.parent.parent.attribute_value("id")
id = id[-38..-1]
korpusu_id.push(id)
end
puts ""
puts "Tagad notiek nepieciešamo korpusu dzēšana..."
progress = ProgressBar.create(:title => "Failu dzēšana:", :progress_mark => "|", :format => "%t [%B] %p%%", :total => korpusu_id)
korpusu_id.each do |korp_id|
#b.goto("#{#env}/CorpusMetadataEditor.aspx?id=#{korp_id}")
#b.execute_script("window.confirm = function() {return true}")
delete_poga = #b.link(:id, "ctl00_ContentPlaceHolder1_lnkDeleteCorpora")
if delete_poga.exists?
delete_poga.click
else
puts "Korpuss\n#{#env}/CorpusMetadataEditor.aspx?id=#{korp_id}\nlietotājam #{lietotajs} nav pieejams rediģēšanai.\nTurpinu ar nākošo korpusu!\n---------------------------------------------"
next
end
container.div(:class => "resItem").wait_until_present
progress.increment
end
I found the final solution for my problem. I changed:
progress = ProgressBar.create(:title => "Failu dzēšana:", :progress_mark => "|", :format => "%t [%B] %p%%", :total => korpusu_id.length)
and this worked for me.
Sorry for spam...

Ruby - loop through array

In a form I have a list of products from database and every product has these items in the form:
= text_field_tag 'item['+product.id.to_s+'][]'
= text_field_tag 'item_value1['+product.id.to_s+'][]'
= text_field_tag 'item_value2['+product.id.to_s+'][]'
I am trying to loop over the array and get all those (item, item_value1, item_value2) this way:
params[:item].each_with_index do |val, index|
puts "#{val[0]} => #{val[1]}"
end
and the output is like:
191359 => [""]
191361 => [""]
191360 => ["15"]
191212 => [""]
191210 => ["9"]
248974 => [""]
191209 => [""]
190920 => [""]
190919 => [""]
190921 => [""]
But, how to get all data for the respective products? Something like
puts "item: #{item}, item_value1: #{item_value1}, item_value2: #{item_value2}"
There are three parameters here item, item_value1 and item_value2. Iterating over item will give you values from parameter items only and not from intem_value1 and item_value2. If the indexes of these 3 parameters are relative, then you can play with index as in your code
params[:item].each_with_index do |val, index|
puts "#{params[:item_value1][index]} => #{params[:item_value1][index]}"
end

How to pass a hash object to a HAML tag

Please consider this example:
- user_links_params = _user_link_params(current_user)
%a{ :'data-msgstore-path' => user_links_params[:'data-msgstore-path'],
:'data-user_id' => user_links_params[:'data-user_id'],
:class => user_links_params[:class],
}
/ too many html tags and stuff to fit in a simple link_to
I would be nice to fit all this in a simple statement like the following:
%a[_user_link_params(current_user)]
/ too many html tags and stuff to fit in a simple link_to
Yes, it's possible, and you were close:
%a{_user_link_params(current_user)}
From the HAML reference:
For example, if you defined
def hash1
{:bread => 'white', :filling => 'peanut butter and jelly'}
end
def hash2
{:bread => 'whole wheat'}
end
then %sandwich{hash1, hash2, :delicious => true}/ would compile to:
<sandwich bread='whole wheat' delicious='true' filling='peanut butter and jelly' />

How can I get all the checked items from a submitted form with sinatra's params?

I'm running Sinatra 1.0 with HAML, my form has a number of checkboxes, for example books I like, and you would select all the books you want. The checkbox name is "books".
In sinatra params['books'] there should be an array of all the books that were checked, but it only has the last item that was checked, not an array.
How can I get all the checked items?
HAML:
%form{:action => "/test", :method => 'post'}
%input{:name=>'check',:type=>'checkbox',:value=>'item1'} item 1
%input{:name=>'check',:type=>'checkbox',:value=>'item2'} item 2
%input{:name=>'check',:type=>'checkbox',:value=>'item3'} item 3
%input{:type => "submit", :value => "send", :class => "button"}
Sinatra get method
post '/test' do
puts params['check'] #should be an array but is last item checked
end
Very close, but do not but numbers in the arrays
%form{:action => "/test", :method => 'post'}
%input{:name=>'check[]',:type=>'checkbox',:value=>'item1'} item 1
%input{:name=>'check[]',:type=>'checkbox',:value=>'item2'} item 2
%input{:name=>'check[]',:type=>'checkbox',:value=>'item3'} item 3
Now,
post '/test' do
puts params['check'] #puts an array of what was checked to stdout
end
Wouldn't that output a bunch of checkboxes with the same name? If so, params['check'] is probably getting replaced with each new checkbox.
Try naming each one something different. If you really want it in an array, try hacking the names:
%input{:name=>'check[1]',:type=>'checkbox',:value=>'item1'} item 1
%input{:name=>'check[2]',:type=>'checkbox',:value=>'item2'} item 2
%input{:name=>'check[3]',:type=>'checkbox',:value=>'item3'} item 3
Try
%input{:type => "checkbox", :value => "1", :name => "checkbox[]", :id => "id1"} Chk1
%input{:type => "checkbox", :value => "2", :name => "checkbox[]", :id => "id2"} Chk2
%input{:type => "checkbox", :value => "3", :name => "checkbox[]", :id => "id3"} Chk3
Then in the rails or sinatra
puts params[:checkbox]
Then you can see the checked items.

Resources