25 Feb 2009, 11:36
Generic-user-small

Björn Großmann (2 posts)

Thanks a lot for the great book! I am doing RSpec and Cucumber so I can hardly wait for you to write the missing chapters! Thanks for putting out V2 of the beta so fast!

One of the cool new features of Rails 2.3 is the build in ability to use nested forms. I have implemented this using the “old” way as described by Ryan here: http://railscasts.com/episodes/75

I would like to know how I can use webrat with the fill_in method to fill in an array of text-fields each having the same name. For instance I have html code that looks like this:

… followed by the same line

Note that I have two text fields with the same name and ID. I know this is bad. Is there a way to tell webrat to fill in the second occurance of a field with a certain name instead of the first one?

Has anybody tried this with the built in feature of Rails 2.3? (As I said, I used the approach suggested by Ryan in his Railscast)

Any ideas are greatly appreciated. I could imagine that this may be of general interest as more people will be using nested forms now that it is supported by Rails, so it may be worth including this in the book.

Regards, Björn

25 Feb 2009, 17:54
Generic-user-small

Zach Dennis (3 posts)

Bjorn,

Rails edge has changed how nested forms are built. They no longer duplicate ids and names. See http://weblog.rubyonrails.org/2009/1/26/nested-model-forms for more information.

In general, if/when there are times where you need help picking a specific element on the page you can use Webrat’s #within method to scope the selection. For example, if there are two input fields with the label “Name” you can scope them via a CSS selector:

within '.foo' do 
  fill_in "Name", :with => "foo"
end

within '.bar' do
  fill_in "Name", :with => "foo"
end

You should also be able to rely on CSS 3 pseudo selectors, ie: within(‘.item:last-of-type’), however nokogiri appears to have an issue right now when working with pseudo selectors in some cases. Also, currently Webrat only makes #within available when running features.

In view specs you should be able to rely on nested calls to have_selector to verify certain things exist:

response.should have_selector(".foo:first-of-type") do |foo|
   foo.should have_selector("input", :name => "bar")
end

response.should have_selector(".foo:last-of-type") do |foo|
   foo.should have_selector("input", :name => "bar")
end

Again, the nokogiri issue withstanding, some pseudo selectors may fail right now.

26 Feb 2009, 10:44
Generic-user-small

Björn Großmann (2 posts)

Zach, thank you so much for your reply. I will probably use the built in functionality of Rails 2.3 for this. It is good to know that it does not dublicate the ids and names! The “within” syntax is nice, too!! Thanks

01 Mar 2009, 20:47
Generic-user-small

Zach Dennis (3 posts)

Bjorn,

To add to my earlier comment. Although the latest changes in Rails 2.3 helps protect against duplicating ids and names there are still cases where you will end up with duplicate ids and names. Specifically for forms that you add to the page via javascript. In these cases I find #within very helpful to narrow things down.

  You must be logged in to comment