Can Abingo Test run on haml? - haml

I am using Abingo to do ABtesting on my website. So it can generate different views as A and B. But all examples I find are using .erb, like this:
<% ab_test("signup_title", ["Sign up", "Registration", "Free Sign up"], :conversion => "signup") do |signup_title| %> <% title "Free Sign up" %> <% end %>
Can I use ABingo to switch views in .haml? If so, what syntax should I use?
Thanks a lot.

The conversion to HAML for your example is:
- ab_test("signup_title", ["Sign up", "Registration", "Free Sign up"], :conversion => "signup") do |signup_title|
- title "Free Sign up"
I assume you as following the ABingo Railscast. Just be aware that these screencasts use the Nifty Layout generator and have a title method.

Related

simple_form error messages do not go away

I'm using simple_form with twitter bootstrap on Rails.
Everything works great, except when showing live validations in a form-inline class. My code for the form is:
<%= simple_form_for #message,
url: mailing_list_path,
html: { class: "form-inline" },
method: :post,
validate: true do |f| %>
<%= f.input_field :email_address, label: false %>
<%= f.submit "Submit" %>
<% end %>
This shows the error message properly (e.g. "is invalid"), but if I click off the input and then back on again, it adds another message (e.g. it would say "is invalid is invalid"). For example, two sequential invalid entries and then a blank entry would give:
Is there any way to have simple_form remove the existing error message before adding a new one?
EDIT:
I solved this using some ghetto js, but would still like to know if the functionality I mentioned above is built in. The divs are still there, they're just hidden instead of all showing together. Would be great to have them actually removed by the form validation...
$('input.email-address-input').on 'keyup', () ->
$(this).parent('form').siblings('.help-inline').hide()

Redirecting outside a web app?

This is my first time developing a rails app from scratch. The goal of my code is to use a title and link (both stored in a database table) to redirect users to the link. (Problem) When I click the link title, I'm redirected to localhost:3000/google.com instead of google.com. (Assuming google.com was the value in link.link)
<h1>Links#index</h1>
<% #links.each do |link| %>
<p>
<%= link_to link.title, link.link %>
</p>
<% end %>
Notes:
(1) Using Rails 3.1
(2) The contents of my routes.rb file are below (Not sure if the use of resources :links has something to do with my problem)
CodeHed::Application.routes.draw do
resources :links
get "links/index"
root :to => "links#index"
end
Are your links prefixed with "http://"? If not, try adding that in programmatically with something like:
def add_http(link)
if (link =~ /http(?:s)?:\/\//)
link
else
"http://#{link}"
end
end
If that doesn't work then you could simply enter raw html:
<h1>Links#index</h1>
<% #links.each do |link| %>
<p>
<%= link_to title, add_http(link) %>
</p>
<% end %>
(I haven't checked this code)

Email send to friend functionality?

I'm trying to set up a mail-to-friend function from my Home Page so when the user clicks the email image their client opens up and has the title and message body already filled in. Any help would be appreciated?
<%= link_to image_tag("facebook.png"), "http://www.facebook.com/site" %>
<%= link_to image_tag ("twitter.png"), "http://twitter.com/#!/site" %>
<%= link_to image_tag "email.png" ???? %>
Here is the best way to achieve this according to the Rails api:
mail_to "me#domain.com"
# => me#domain.com
Full documentation at: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-mail_to
Use a mailto tag
http://www.w3schools.com/html/tryit.asp?filename=tryhtml_mailto
Syntax here: http://www.ianr.unl.edu/internet/mailto.html

Cucumber click only working when #javascript is used?

I know there's a simple fix for this but for the life of me I can't remember what it is.
My feature file is as following:
Scenario: Editing locations
When I edit "Western Australia"
And fill in "Name" with "Tasmania"
And I press "Save"
Then I should see a form success message
And I've defined the 'Edit' step as the following:
When /^I edit "([^"]*)"$/ do |name|
within(:xpath, "//tr[./td[contains(text(), '#{name}')]]") do
find(:css, "a img[alt=Edit]").click
end
end
The HTML for the index page it works on is as follows:
<tr>
<td>Western Australia</td>
<td>WA</td>
<td>
<img alt="Edit" src="/images/icons/pencil.png" title="Edit" />
</td>
</tr>
And then the form HTML:
<%= semantic_form_for [:admin, #location] do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :abbreviation %>
<% end %>
<%= f.submit "Save" %></li>
<% end %>
As it is, it doesn't work - I get the following error:
And fill in "Name" with "Tasmania" # features/step_definitions/web_steps.rb:39
cannot fill in, no text field, text area or password field with id, name, or label 'Name' found (Capybara::ElementNotFound)
But the form element 'name' is clearly there on the page.
If I add 'Then show me the page' before the 'Fill in' then capybara saves the index page, leading me to think it isn't getting to the edit form at all.
... Yet if I add the '#javascript' tag to the feature, it works perfectly, even though there is no Javascript on this page.
I've solved this once before, but for the life of me I can't work out how...
Well I managed to solve the problem - the issue was with my CSS selector that was clicking the 'Edit' link. I don't know why it didn't work as-was, but I changed the find(:css, "a img[alt=Edit]").click to read click_link('Edit') and it worked perfectly.
Source: http://groups.google.com/group/ruby-capybara/browse_thread/thread/9c997395306d40e2/
For starters, you need to actually "visit" the edit page using the Capybara visit method within your When block. Also, I don't believe you want to use fill_in for inserting text into your tags (at least according to the error message it is only for text fields/areas).

Syntax for link_to with block in rails3 with :remote=>true and including :class and :id

For the love of god, I've been banging my head on this for hours. Using rails3 rc, 1.9.2.
I'm trying to create a link_to that submits an ajax request, with parameters, a class and id, and needs a block so I can insert a span tag around the name. Documentation is of absolutely zero help, as are numerous google searches. Here's what I've got so far:
<%= link_to(
:url=>{
:controller => 'themes', :action => 'remove_tag',
:entity_id => entity_id, :theme_id => theme_id,
:entity => entity, :element_id => element_id, :parent_id=>parent_id
},
:remote => true,
:id => "theme-tag-#{entity}-#{entity_id}",
:class => "tag") do %>
<span class='subtract'><%= tag %></span>
<% end %>
The generated url looks like this:
<a href="/explore/index/theme-tag-user-3?url[controller]=themes&url[action]=remove_tag&url[entity_id]=3&url[theme_id]=16&url[entity]=user&url[element_id]=filter-contributor-3&url[parent_id]=filter-contributors&remote=true&class=tag">
Test descriptor
I can't indicate properly that the text "Test descriptor" is actually properly included within the span; code formatting is failing a little here, however, the href is wrong, there's no class or id, and downhill it continues to roll
If I didn't need the block, I could just add the name and not have to specify :url=>{...} (leaving if off throws an exception with the block, go figure) and then follow that with :remote=>true, :id=>"whatever", :class=>"blah" and it works. What am I doing wrong? Because I'm new to rails in general, I'd also like to understand why this syntax must differ so much? I mean really, thank god you don't have to write a lot of links like this in a web app... ;-)
Thanks in advance
turns out you have to do url_for(...) instead of :url=>{...} and it all worked as expected.
Just putting wkhatch's comment here so it's nicely formatted.
<%= link_to(
url_for(:controller=>'themes',
:action=>'remove_tag',
:entity_id=>entity_id,
:theme_id=>theme_id,
:entity=>entity,
:element_id=>element_id,
:parent_id=>parent_id),
:remote=>true,
:id=>"theme-tag-#{entity}-#{entity_id}") do %>
<span class='subtract'></span><%= tag %>
<% end %>