Capybara selecting option from drop down - ruby

I have reviewed a number of solutions from stackoverflow to no avail. I was hoping someone could help me understand the error of my ways.
The haml markup looks like:
.graduation-date-range
%label Graduation Date Range
.row-fluid
.span6
.control-group
.controls
.bfh-selectbox{:id => "graduation_start_year", :style => "width: 100%"}
= f.hidden_field :graduation_start_date
%a.bfh-selectbox-toggle{"data-toggle" => "bfh-selectbox", :href => "#", :role => "button"}
%span.bfh-selectbox-option Start Year
%b.caret
.bfh-selectbox-options
%div{:role => "listbox"}
%ul.start-years{:role => "option"}
- (2012..2018).each do |y|
%li
%a{"data-option" => y, :href => "#"}= y
.help-inline
.span6
.control-group
.controls
.bfh-selectbox{:style => "width: 100%"}
= f.hidden_field :graduation_end_date
%a.bfh-selectbox-toggle{"data-toggle" => "bfh-selectbox", :href => "#", :role => "button"}
%span.bfh-selectbox-option End Year
%b.caret
.bfh-selectbox-options
%div{:role => "listbox"}
%ul.end-years{:role => "option"}
- (2012..2018).each do |y|
%li
%a{"data-option" => y, :href => "#"}= y
.help-inline
The solution I am currently trying to employ:
select('2012', :from => 'graduation_start_year', :visible => false)
I receive :
Capybara::ElementNotFound:
Unable to find select box "graduation_start_year"
My understanding is that either the label, name, or ID can be used to select from but I may be mistaken.
Thank you in advance to those who respond.

It appears that this is not a form select box, but an html list that has been turned into something that resembles a dropdown box via javascript and CSS.
capybara select is looking for <select> and <option> tags. This is going to require a js type test with selenium or poltergeist to test.

Related

Passing Hash as Instance Variable in Get Method In Sinatra

I'm creating a metro application for a project. I'm attempting to pass a hash containing the options that a user can select as the key and the url it will redirect to as the value. When I do, sinatra reports:
no implicit conversion of Array into String
Which is strange, because I do not have an array so far in my application. I have tried commenting out the embedded ruby in the index.erb file, but that didn't help. The application is as follows:
require "sinatra"
require "sinatra/reloader"
red = {
"Woodley Park" => '/woodley_park',
"Dupont Circle" => '/dupont_circle',
"Farragut North" => '/farragut_north',
"Metro Center" => '/metro_center',
"Union Station" => '/union_station'
}
turquoise = {
"Crystal City" => '/crystal_city',
"Metro Center" => '/metro_center',
"Shaw-Howard" => '/shaw_howard',
"Beltwater" => '/beltwater'
}
orange = {
"Farragut West" => '/farragut_west',
"McPherson" => '/mcpherson',
"Metro Center" => '/metro_center',
"Federal Triangle" => '/federal_triangle',
"Smithsonsian" => '/smithsonsian',
"L'efant Plaza" => '/lefant_planza'
}
lines = {
"Red" => "/red_line",
"Turquoise" => "/turquoise_line",
"Orange" => "/orange_line"
}
get '/' do
#header = "Welcome to the D.C. Metro! Which line will you be starting at?"
#options = lines
end
And the index.erb looks like this. Again, commenting out the relevant lines did not change the results. Thanks in advance!
<h2><%= #header %></h2>
<% #options.each do |name, link| %>
<a href=<%= link %>><%= name %></a>
<% end %>

How to update all fields in MailChimp API batch subscribe using Ruby and Gibbon

I am using Ruby 1.9.3 without Rails and version 1.0.4 of the Gibbon gem.
I have referrals populated with my list and can send the following to MailChimp with Gibbon. However, only the email address and email type fields are populated in the list in MailChimp. What am I doing wrong that is prohibiting all the merge fields from being imported via API?
Here is the batch and map of the list.
referrals.each_slice(3) do |batch|
begin
prepared_batch = batch.map do |referral|
{
:EMAIL => {:email => referral['client_email']},
:EMAIL_TYPE => 'html',
:MMERGE6 => referral['field_1'],
:MMERGE7 => referral['field_2'],
:MMERGE8 => referral['field_3'],
:MMERGE9 => referral['field_4'],
:MMERGE11 => referral['field_5'],
:MMERGE12 => referral['field_6'],
:MMERGE13 => referral['field_7'],
:MMERGE14 => referral['field_8'],
:MMERGE15 => referral['field_9'],
:FNAME => referral['client_first_name']
}
end
#log.info("prepared_batch : #{prepared_batch}")
result = #gibbon.lists.batch_subscribe(
:id => #mc_list_id,
:batch => prepared_batch,
:double_optin => false,
:update_existing => true
)
#log.info("#{result}")
rescue Exception => e
#log.warn("Unable to load batch into mailchimp because #{e.message}")
end
end
The above executes successfully. However, only the email address and email type are populated but most of the fields should be populated.
Here is my log output for one of the prepared_batches. I replaced the real values with Value. I used my own email for testing.
I, [2013-11-11T09:01:14.778907 #70827] INFO -- : prepared_batch : [{:EMAIL=>
{:email=>"jason+6#marketingscience.co"}, :EMAIL_TYPE=>"html", :MMERGE6=>"Value",
:MMERGE7=>"Value", :MMERGE8=>nil, :MMERGE9=>nil, :MMERGE11=>"8/6/13 0:00",
:MMERGE12=>"Value", :MMERGE13=>nil, :MMERGE14=>"10/18/13 19:09", :MMERGE15=>"Value",
:FNAME=>"Value"}, {:EMAIL=>{:email=>"jason+7#marketingscience.co"}, :EMAIL_TYPE=>"html",
:MMERGE6=>"Value", :MMERGE7=>"Value", :MMERGE8=>nil, :MMERGE9=>nil, :MMERGE11=>"8/6/13
0:00", :MMERGE12=>"Value", :MMERGE13=>nil, :MMERGE14=>nil, :MMERGE15=>"Value",
:FNAME=>"Value"}, {:EMAIL=>{:email=>"jason+8#marketingscience.co"}, :EMAIL_TYPE=>"html",
:MMERGE6=>"Value", :MMERGE7=>"Value", :MMERGE8=>nil, :MMERGE9=>nil, :MMERGE11=>"8/7/13
0:00", :MMERGE12=>"Value", :MMERGE13=>nil, :MMERGE14=>nil, :MMERGE15=>"Value",
:FNAME=>"Value"}]
Here is the log output of result from the MailChimp call.
I, [2013-11-11T09:01:14.778691 #70827] INFO -- : {"add_count"=>3, "adds"=>
[{"email"=>"jason+3#marketingscience.co", "euid"=>"ab512177b4", "leid"=>"54637465"},
{"email"=>"jason+4#marketingscience.co", "euid"=>"eeb8388524", "leid"=>"54637469"},
{"email"=>"jason+5#marketingscience.co", "euid"=>"7dbc84cb75", "leid"=>"54637473"}],
"update_count"=>0, "updates"=>[], "error_count"=>0, "errors"=>[]}
Any advice on how to get all the fields to update in MailChimp is appreciated. Thanks.
Turns out the documentation for using the Gibbon gem to batch subscribe is not correct. You need to add the :merge_vars struct to contain the fields other than email and email type. My final code looks like the following. I'm also going to update this code in its entirety at: https://gist.github.com/analyticsPierce/7434085.
referrals.each_slice(3) do |batch|
begin
prepared_batch = batch.map do |referral|
{
:EMAIL => {:email => referral['email']},
:EMAIL_TYPE => 'html',
:merge_vars => {
:MMERGE6 => referral['field_1'],
:MMERGE7 => referral['field_2'],
:MMERGE8 => referral['field_3'],
:MMERGE9 => referral['field_4'],
:MMERGE11 => referral['field_5'],
:MMERGE12 => referral['field_6'],
:MMERGE13 => referral['field_7'],
:MMERGE14 => referral['field_8'],
:MMERGE15 => referral['field_9'],
:FNAME => referral['first_name']
}
}
end
#log.info("prepared_batch : #{prepared_batch}")
result = #gibbon.lists.batch_subscribe(
:id => #mc_list_id,
:batch => prepared_batch,
:double_optin => false,
:update_existing => true
)
#log.info("#{result}")
rescue Exception => e
#log.warn("Unable to load batch into mailchimp because #{e.message}")
end
end

Favicon not showing with camping

I want to learn about webapps. I decided to learn by doing and chose to start simple with Camping as (i). it is small & (ii). i know some ruby.
The favicon is not showing. I am using a favicon taken from another site so it know its file format is valid.
Here is the code from the controller.
class Favicon < R '/favicon\.ico'
# Load the favicon into memory
FAVICON = File.read('favicon.ico')
def get
#headers['Content-Type'] = "image/x-icon"
FAVICON
end
end
Here is the code from the view:
I purposely placed a link to the favicon twice as an experiment. No joy.
def layout
html do
head do
title 'Custom Made Kameez'
link :rel => 'icon', :href => 'favicon.ico', :type => 'image/x-icon'
link :rel => 'shortcut icon', :href => 'favicon.ico', :type => 'image/x-icon'
link :rel => 'stylesheet', :type => 'text/css', :href => '/styles.css', :media => 'screen'
end
I have tried clearing my cache and using Firefox and IE, same issue.
Two problems as far as I can see.
You should use an absolute path to the favicon:
link :rel => 'icon', :href => '/favicon.ico', :type => 'image/x-icon'
You should read the file as binary:
FAVICON = File.binread('favicon.ico')
I took your code and it worked just fine, and that was before I uncommented the link directives: browsers are just really keen to go get /favicon.ico .
Is your problem the fact that your favicon never showed in the first place, or that your original favicon is now stubbornly refusing to change?

Does OpenStruct has support for nesting ? If Yes, How can I construct XML using Nokogiri? If not, Can this be done using Nokogiri and JSon?

The Earlier solution provided by #Jamie seems to be working fine for only few scenario's. The solution is
require 'nokogiri'
require 'ostruct'
xml = <<-'XML'
<Catalog>
<Interface></Interface>
<Dialog></Dialog>
<Manifest></Manifest>
</Catalog>
XML
collection = [
OpenStruct.new(book: '1984', pen: 'George Orwell'),
OpenStruct.new(book: 'Thinking, Fash and Slow', pen: 'Daniel Kahneman')
]
doc = Nokogiri::XML(xml)
catalog = doc.root
node_set = Nokogiri::XML::NodeSet.new(doc)
collection.each do |object|
book = Nokogiri::XML::Node.new('Book', doc)
book_author = Nokogiri::XML::Node.new('Book_Author', doc)
book.content = object.book
book_author.content = object.pen
node_set << book
node_set << book_author
end
catalog.first_element_child.before(node_set)
puts doc.to_xml
While I have the below XML to be constructed using Nokogiri. I just wanted to know, if this can't be done using OpenStruct. Could this be done using Json? If Yes, Can you please suggest me how to construct this using Nokogiri and Json.
Below is this XML, I want to construct...
<CatalogOrder>
<CatalogStoreNumber>657758</CatalogStoreNumber>
<CatalogStoreId>CTH6536</CatalogStoreId>
<CatalogStoreLocation>UnitedStates</CatalogStoreLocation>
<CatalogOwnerId>TYCT11190</CatalogOwnerId>
<CatalogOwner>McGrawHill Pub.</CatalogOwner>
<CatalogList>
<CatalogProductInfo>
<ProductType>Book</ProductType>
<ProductName>The Client</ProductName>
<ProductId>CRSUS113246A</ProductId>
<ProductCategory>Crime And Suspense</ProductCategory>
<ProductSubCategory>Vintage Books</ProductSubCategory>
<ProductPrice>45.50 USD</ProductPrice>
<ProductAuthor>
<AuthorFirstName>John Grisham</AuthorFirstName>
<AuthorMiddleName>Willbur</AuthorMiddleName>
<AuthorContact>19876648981</AuthorContact>
</ProductAuthor>
</CatalogProductInfo>
<CatalogProductInfo>
<ProductType>Pen</ProductType>
<ProductName>Reynolds</ProductName>
<ProductId>PRREY546647</ProductId>
<ProductCategory>Misc. Stationary</ProductCategory>
<ProductSubCategory>Stationary Items</ProductSubCategory>
<ProductPrice>3.00 USD</ProductPrice>
<ProductAuthor>
<AuthorFirstName>Ryan Reynolds</AuthorFirstName>
<AuthorMiddleName>William</AuthorMiddleName>
<AuthorContact>16577589898</AuthorContact>
</ProductAuthor>
</CatalogProductInfo>
<CatalogListType>ProductCollection</CatalogListType>
<CatalogListSource>Web</CatalogListSource>
</CatalogList>
<CatalogVerificationLog>
<CatalogVerificationStatus>Verified</CatalogVerificationStatus>
<CatalogVerificationDateTime>2012-03-12T13:00:15+5:30</CatalogVerificationDateTime>
<CatalogVerificationId>64774A</CatalogVerificationId>
<CatalogVerificationRequestedBy>User_121</CatalogVerificationRequestedBy>
</CatalogVerificationLog>
</CatalogOrder>
I have data that is organized in OpenStruct format below...
require 'ostruct'
require 'nokogiri'
collection = [
OpenStruct.new(:catalogStoreNumber => '657758',
:catalogStoreId => 'CTH6536',
:catalogStoreLocation => 'UnitedStates',
:catalogOwnerId => 'TYCT11190',
:catalogOwner => 'McGrawHill Pub.',
:catalogList => OpenStruct.new(
:catalogProductInfo => OpenStruct.new(
:productType => 'Book',
:productName => 'The Client',
:productId => 'CRSUS113246A',
:productCategory => 'Crime And Suspense',
:productSubcategory => 'Vintage Books',
:productPrice => '45.50 USD',
:productAuthor => OpenStruct.new(
:authorFirstName =>'John Grisham',
:authorMiddleName=> 'Willburt',
:authorContact => '19876648981')),
:catalogProductInfo => OpenStruct.new(
:productType => 'Pen',
:productName => 'Reynolds',
:productId => 'PRREY546647',
:productCategory => 'Misc. Stationary',
:productSubcategory => 'Stationery Items',
:productPrice => '3.00 USD',
:productManufacturer => 'Reynolds Inc.',
:productAuthor => OpenStruct.new(
:authorFirstName => 'Ryan Reynolds',
:authorMiddleName => 'William',
:authorContact => '16577589898')),
:catalogListType => 'ProductCollection',
:catalogListSource => 'Web'
),
:catalogVerificaitionLog => OpenStruct.new(
:catalogVerificationStatus => 'Verified',
:catalogVerificationDateTime => '2012-03-12T13:00:15+5:30',
:catalogVerificationId => '64774A',
:catalogVerificationRequestedBy => 'user_121')
)]
I am not sure whether it is possible with OpenStruct, as it lacks Nesting capabilities. Is there any other Way to use JSon to achieve this without any outstanding limitations ? Please correct my earlier code.
To see this earlier question, How to Add child Nodes in NodeSet using Nokogiri
Click [here] (http://stackoverflow.com/questions/10300095/how-to-add-child-nodes-in-nodeset-using-nokogiri)
I see below issues with OpenStruct...
i want to access the "productType" of first "catalogProductInfo" and i said "collection.catalogList.catalogProductInfo.productType.content" and I get the below error
"undefined method `productType' for # (NoMethodError)
I got stuck here and want to get out if this situation. If data can't be organized using OpenStruct. Can this be done using JSon. ? Also suggest me the way to do it Nokogiri and JSon. Help me out.

Getting best_in_place gem to co-operate with Redcarpet markdown

For my markdown text i normally do the following on the text content(#user.content):
<%= raw Redcarpet.new(#user.content).to_html.html_safe %>
However i am now using the best_in_place gem like this
<%= best_in_place(#question, :statement, :path => {:controller => "users", :action => "update"}, :type => :textarea) %>
How can i integrate Redcarpet with this? i have tried this:
<%= Redcarpet.new(best_in_place(#question, :statement, :path => {:controller => "users", :action => "update"}, :type => :textarea)).to_html.html_safe %>
however the output is not what is expected. The best_in_place readme seems to point to a way to solve this, but i don't understand it here, under the section titled "Using custom display methods". Can someone explain to me how i can make this work?
I have never used the best_in_place gem, so I don't have tried it out. But from the documentation, you should do the following steps:
Add to the call to best_in_place the additional argument :display_as.
Implement the method referenced by :display_as in your model object.
Here are the details:
Additional argument
%= best_in_place(#question, :statement, :display_as => 'mk_statement', :path => {:controller => "users", :action => "update"}, :type => :textarea) %>
Implementation in the model
class Question
require 'redcarpet'
def mk_statement
Redcarpet.new(self.statement).to_html.html_safe
end
You can also use the display_with attribute and pass it a proc like so:
<%= best_in_place(#question, :statement, :display_with => proc { |v| Redcarpet.new(v).to_html.html_safe, :path => {:controller => "users", :action => "update"}, :type => :textarea) %>

Resources