In a form, I manually named a label:
<%= f.label :name, "DEA License Number" %>
<%= f.text_field :dea_license_number, class: 'form-control' %>
The HTML looks like this:
<label for="dentist_detail_name">DEA License Number</label>
<input class="form-control" type="text" name="dentist_detail[dea_license_number]" id="dentist_detail_dea_license_number" />
And my system test:
fill_in "DEA License Number", with: "999999"
When I run the tests, I get this:
Error:
SignUpJobSeekersTest#test_Sign_Up_Job_Seekers:
Capybara::ElementNotFound: Unable to find visible field "DEA License Number" that is not disabled
I'm at a bit of a loss. Thanks for supporting a newbie.
When you look at the generated HTML the for attribute of the label element doesn't match the id attribute of the input
'dentist_detail_name' != 'dentist_detail_dea_license_number'
therefore the label is not actually associated with the input (label must either wrap the input or for must match id).
I think there is something wrong with your label. It should be
<%= f.label :dea_license_number, "DEA License Number" %>
Give that a try?
Related
I want to put this: <%= f.text_field :name %>
into the code below, but I keep getting an error. How can I properly embed it so that the code will work. Thanks!
<form>
<div class="form-group">
<input type= class="form-control" id="exampleInputEmail1" placeholder="Enter Value">
</div>
</form>
You dont' need the whole input section, just do this :
<%= f.text_field :name , :class=> "form-control"%>
Just add bootstrap classes into your code via parameter :class
Use Formtastic Bootstrap it's a gem for rails, create forms with automatic bootstrap class
'f' is not instantiated in you code so following code will raise error in the form-
<%= f.text_field :name %>
alternatively you can use this -
<%= text_field_tag :name,'', :placeholder => "Enter Value", :class=>"form-control", :id=>"exampleInputEmail1" %>
this will not raise any error on embedding in the form.
In my Rails 4 application I have specified my locales as follows:
config/locales/views/show_user/en.yml
with content:
en:
activerecord:
attributes:
user:
first_name: First name
last_name: Last name
...
layouts:
header:
home: "Home"
help: "Help"
...
footer:
about: "About"
contact: "Contact"
users:
new:
signup: "Sign up!"
create_my_account: "Create my account"
When I run my application in my localhost, all my labels display correctly. However, my RSpec test,
describe "with valid information" do
before do
fill_in "First name", with: "Jenny"
fill_in "Last name", with: "Johnson"
fails when I make the following change to my partial, /views/users/_fields.html.erb
<%= f.label t :first_name, scope: [:activerecord, :attributes, :user] %>
<% # f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
My RSpec test displays this failure:
1) User pages signup with valid information should create a user
Failure/Error: fill_in "First name", with: "Jenny"
Capybara::ElementNotFound:
Unable to find field "First name"
# ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'
I have noticed that the generated html of my form displays a different id for first_name, but I do not know if this is important:
<label for="user_First name">First name</label>
<input id="user_first_name" name="user[first_name]" type="text" />
<label for="user_last_name">Last name</label>
<input id="user_last_name" name="user[last_name]" type="text" />
My question is therefore: Why can Capybara not find my label, :first_name, while my page displays correctly in my browser?
It is important. Capybara uses the locator argument to fill_in in one several ways:
if there is a text field (or similar input) with that id then it will use that field
if there is a label that contains the text, then it will use the field that label for.
You're in the second case, so capybara uses the for attribute on the label to find the text field. This doesn't work because the label's for is user_First name but the id on the text field is user_first_name.
By default rails derives both the label text and the for attribute of the label from the value you pass as the first argument, so you should pass the attribute name as the first argument and then the human readable translation as the second argument
f.label :first_name, t(:first_name, scope: [:activerecord, :attributes, :user])
My reading of the documentation is that looking for this transaction would actually be the default - you may not need to specify it at all.
So I have seen examples of text_field and text_area being used in forms like this:
<%= form_for :account do |a| %>
Name: <%= a.text_field :name %><br />
Password: <%= a.text_area :password %><br />
Password Confirmation: <%= a.text_field :password_confirmation %><br />
<%= a.submit %>
<% end %>
I don't understand the difference, though. Is it necessary for a beginner Rails developer to understand the difference?
I found some explanations in the API which I don't understand - perhaps somebody can take a look and let me know what is going on.
For "text_area":
text_area(object_name, method, options = {})
Returns a textarea opening and closing tag set tailored for accessing a
specified attribute (identified by method) on an object assigned to the template
(identified by object).
Additional options on the input tag can be passed as a hash with options.
Then, for "text_field":
text_field(object_name, method, options = {}) Link
Returns an input tag of the “text” type tailored for accessing a specified
attribute (identified by method) on an object assigned to the template
(identified by object). Additional options on the input tag can be passed
as a hash with options. These options will be tagged onto the HTML as an
HTML element attribute as in the example shown.
a.text_field :name is parse to the following html
<input type="text" name="name">
a.text_area :name would parse to something like:
<textarea rows="4" cols="50">
</textarea>
depending on the options passed.
The simplest way of looking at it is text_field gives you a place for a single line of text, where text_area gives an area for multiple lines.
you can pass a hash of options to the text_area helper to specify the number of rows and columns.
In the example you give above, it would be poor practice to use either text_field or text_area for passwords, you'd be better to use a.password_field
thats a good answer - funny when googling this and looking for a spot on example - after i read the response above - i looked at my code and realized it was an even better example
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :bio %>
<%= f.text_area :bio %><br />
makes sense, name would only need a single line (unless you have a super long name) where as bio, would need multiple lines.
I would like to add a span element to my i18n localized label in Rails 3.2.3.
This is what I've got:
<%= f.label :address, "<span class=\"optional\">optional</span>".html_safe %>
However, in the output it produces:
<label for="person_address">
<span class="optional">optional</span>
</label>
What I need is this:
<label for="person_address">
Address <span class="optional">optional</span>
</label>
Can anybody tell me how to do this?
use the block form and translate the attribute name "manually" :
<%= f.label :address do %>
<%= f.object.class.human_attribute_name :address %>
<span class="optional">optional</span>
<% end %>
note
The second parameter in the 'label' helper will be the text of the label. If you did this:
<%= f.label :address, "Address <span class=\"optional\">optional</span>".html_safe %>
It would show correctly, I think.
You an also add html to the yml file and use .html_safe on the yml item explicitly it would also work.
<%= f.label :address, t('path.to.label').html_safe %>
I'd also be tempted to try something with javascript and css - to class the field and add something via jquery or with a css :after to the label.
I'm constructing a simple form in ERB but the HTML produced by the text_field tag makes the for attribute in the label tag invalid.
<div>
<p><%= label_tag "email[name]", "Name" %></p>
<%= text_field :email, :name, :class => "text_field" %>
</div>
Produces the HTML
<div>
<p><label for="email[name]">Name</label></p>
<input class="text_field" id="email_name" name="email[name]" size="30" type="text" />
</div>
Which results in the error
character "[" is not allowed in the
value of attribute "for".
How do I generate the text with without the nested parameter name email[name] to change the label tag for attribute? Is there an alternative approach that produces valid HTML?
The for attribute is supposed to reference the ID attribute of the element for which it is the label, not its name.
Therefore, don't you need:
<div>
<p><%= label_tag "email_name", "Name" %></p>
<%= text_field :email, :name, :class => "text_field" %>
</div>
...?
Take it out of the quotes, or generate the div content as a string and add it to the div.innerHTML