93

I have this:

<input class="string optional" 
    id="course_group_courses_attributes_2_name" 
    name="course_group[courses_attributes][2][name]" 
    placeholder="Lengua" 
    size="15" 
    type="text" />
</div> 

How do I fill in a that field by its ID?

4 Answers 4

136

fill_in accepts the id as first parameter:

fill_in 'course_group_courses_attributes_2_name', :with => 'some text'
6
  • 9
    I thought so, but It is not working. It seems that it doesn't fill it. It doesn't give any errors about not finding it. It just doesn't fill it.
    – Nerian
    Jan 21, 2011 at 13:00
  • Maybe you can include the capybara code in question and any other related code.
    – raidfive
    Jan 21, 2011 at 17:40
  • I discovered that the problem where in Mongoid. Release candidates bugs.
    – Nerian
    Jan 24, 2011 at 23:35
  • @Nerian ah! glad you figured it out
    – raidfive
    Jan 25, 2011 at 0:31
  • have we got the solution?
    – Zakos
    May 27, 2015 at 14:59
34

You can also do it with:

find('Id or class here').set('some text')
8

fill_in accepts id without the # symbol which I found confusing at first. Also note that if your input field is in a modal, you may need to wait for the transition or fade in before the field can be filled in.

0
4

If you're using simple_form gem then you must note that it does automatically id your form elements, hence manually id-ing elements yourself is futile!

There is; however, a naming convention that simple_form follows to id form elements.

object_attribute

So if you're filling-in information for let's say a contact object as follows:

= simple_form_for @contact do |f|
  = f.input :first_name
  = f.input :last_name

simple_form will then automatically generate ids to each form element respectively as so:

id="contact_first_name"
id="contact_last_name"

Now you can simply make use of this naming convention to fulfill all your fill_in needs. Hope this helps.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.