Capybara Choose radio button not working - html

I've done this maybe 100 times but for the life of me I can't understand why it's telling me it can't find this object. Here is my html:
PS I've verified that I'm on the correct page and I can see the advisor when I do a save_and_open_screenshot
HTML
<div class="panel">
<%= form.label :advisor, class: "panel__label" %>
<%= form.radio_button :advisor_or_client, "advisor", class: "panel__input__radio advisor" %>
</div>
<div class="panel">
<%= form.label :client, class: "panel__label" %>
<%= form.radio_button :advisor_or_client, "advisor", class: "panel__input__radio client" %>
</div>
TEST
choose('advisor')
ERROR
Failure/Error: choose('advisor')
Capybara::ElementNotFound:
Unable to find radio button "advisor"
Screenshot
Based on the info provided how can I select the radio button for my feature spec?

choose locates the radio button by name, id or label text - http://www.rubydoc.info/gems/capybara/Capybara/Node/Actions#choose-instance_method . Rather than that you are specifying the value of the input (I'm assuming the second value should be "client" rather than the "advisor" you have). Since you don't have ids, and name would be ambiguous, you can use the option parameter (matches value) to narrow down to the correct radio. Therefore
choose('trade_request_submission[advisor_or_client]', option: 'advisor')
or potentially
choose(option: 'advisor') # if you don't have any other radios with that value
Another option is to specify the label text but that would require adding the ids to the input elements that match the for attributes on the labels to link them correctly, in which case you could do
choose('Advisor')

Related

about the use of id and class: should the ids be always unique in a page?

My application offers the opportunity to users to join events.
In my events index page, I iterate through all the events and show them wrapping each one in a li tag. The user can join an event by clicking on the following button:
<%= form_for(current_user.attendances.build) do |f| %>
<div><%= f.hidden_field :event_id, value: event.id %></div>
<%= f.submit "Join the event", class: "btn btn-primary" %>
<%= f.label :car, class: "checkbox inline checkbox-car" do %>
<%= f.check_box :car %>
<span> I want to share my car </span>
<% end %>
<% end %>
The html corresponding to the chekbox is:
<input id="attendance_car" type="checkbox" value="1" name="attendance[car]">
The problem is the id assigned automatically to the input tag, attendance_car: each event has the same id. Wouldn't this break the rule that the id value must be unique within the HTML document?
I wonder what is the consequence of using in the event partial ids in place of classes, as I did by mistake with some tags: ids would not be unique, but I noticed the application would work.
"Should the id be always unique in a page?" Yes, of course, any DOM element should have a unique id (if provided with one). See w3c for reference on id attribute.
Why don't you simply add a number to your ids to make them unique?
Modern browsers support multiple elements having same ID. This is no valid html and may affect google rankings. If you must have this by side effects of html helpers generating tags, don't bother, since nothing will be broken, unless you are using things like getElementById.

Select options with new lines issue

I have the following rails code (it uses simple form):
<%= f.input :color, collection: custom_colors.split(/\n/).reject(&:empty?) %>
custom_colors comes from a text area where users are asked to enter entries seperated by new lines.
The issue: This displays correctly, however, when a user goes to edit, the color is not saved and the select is empty. I'm using a typical rails scaffold.
The issue (continued): The reason I believe this is not working, is that when saved, the color looks like blue\r\n. However, when I do:
<%= field.options.split(/\n/).reject(&:empty?) %> it returns blue\r, which means the input won't match, as blue\r\n clearly does not equal blue\r
How do I fix this?
Try this in your form.
<%= f.input :color, collection: custom_colors.split(/\n/).map(&:strip).reject(&:empty?) %>

Ruby on Rails pass button value to next page

I am trying to design a webpage that contains both a radio button field and a list of links. When one of the links is clicked, I would like to pass the associated value of the button to a controller. So far I have the following code:
In my views page:
<div id="radio_buttons">
<%= radio_button_tag 'radioGroup', "1" %> 1
<%= radio_button_tag 'radioGroup', "2" %> 2
<%= radio_button_tag 'radioGroup', "4" %> 4
</div>
...
<div class = "text">
<%= link_to "mylink", search_path(:radioGroup => #radioGroup)%>
</div>
In my Controller:
#radioGroup = params[:radioGroup]
When I add the line 'puts #radioGroup' to my controller, it prints nothing. I have been able to pass different values to a new web page upon a link click (i.e those contained in a text field) but for some reason I can't figure out how to do the same thing with buttons. Does anyone know how to accomplish this?
Thanks!
If you want to submit multiple values to another page you should use an HTML form for it. Check this page for more related info: http://guides.rubyonrails.org/form_helpers.html.

HTML / ERB - Standard-Button? [duplicate]

This question already has answers here:
How to make enter the submit button in a form
(3 answers)
Closed 7 months ago.
In the picture you can see 2Buttons: screenshot
I want the user to just type in the email and password and accept it by pressing return.
Is there an opportunity to set this up?
I know that there is a option in C#, but I forgot how it's called. (Standard-Button?)
There are 2 input-texts and 2buttons (Login-Form / Register)
eMail - Password - Login-Btn and or-Register-btn.
The user types in his email and password. After that he press "return" to accept and he is logged in.
How is the options called that the login-btn is the ... first button to choose by pressing "return"?***
That's what I am using:
Ruby on Rails 4
HTML
HTML.ERB
Bootstrap 3
You may have an answer, but to help you understand it, you need to remember Rails renders everything as HTML on the browser-side; so every element you want to display has to be some sort of HTML object
If you want to show a button in Rails, there are a number of ways to do it:
button_to
This creates a simple form which points to a URL. The form has a button element inside, making it look like a pure HTML button to the user:
<%= button_to "New", action: "new" %>
# => "<form method="post" action="/controller/new" class="button_to">
# <div><input value="New" type="submit" /></div>
# </form>"
f.submit
For your specific issue, I think you'll benefit from f.submit, which basically adds a submit button to your form:
<%= form_for #var do |f| %>
<%= f.submit "Register" %>
<% end %>
If you'd like to style this, you'll be able to apply classes to the button directly:
<%= f.submit "Text", class: "class" %>

Can I have inputs without forms in a Rails view?

I want to have a few check boxes on my page, and a link that accesses the value of these check boxes to pass in a link_to helper. I did not want to use a form because the view essentially has a number of links interspersed, and it doesn't naturally seem to be a form with one logical submit button.
I have something like
<% for p in #some_array %>
<!--other stuff .... -->
<input value=<%= p.id %> id=<%= p.id %> name="selected[]" type=checkbox>
<!--other stuff .... -->
<%= link_to "View all selected thing(s)", :action => 'show_selected', :selected_things => selected[] %>
But it doesn't seem to recognize the variable selected which stores the inputs. It raises
undefined local variable or method `selected' for #<#<Class:0x000001021b4a38>:0x00000102319a90>
I would say the last line in your code snippet is causing the error:
<%= link_to "View all selected thing(s)", :action => 'show_selected', :selected_things => selected[] %>
As ruby complains about a selected not being defined.
(Although I can't see why you don't want to use a form, as forcing the selections into something you can pass to the link would require some javascript magic.)