Question

Just for fun, I wrote a very small rails blog (just a hello world). Now I want to create a post using mechanize. So I created a Ruby Prog and started coding.

Here is my problem: Rails creates my form element including all inputs. In HTML my inputs look like this:

<input type="text" size="30" name="post[title]" id="post_title">

or

<textarea rows="20" name="post[description]" id="post_description" cols="40"></textarea>

Well... Here is my Ruby Prog using Mechanize:

require 'rubygems'
require 'mechanize'

agent = WWW::Mechanize.new

page = agent.get('http://localhost:3000/posts/new')
target_form = page.form_with(:class => 'new_post')
target_form.post[title] = "test"
target_form.post[description] = "test"
page = agent.submit(target_form)
puts "end"

I know where my error is but I don't know how to fix it. At target_form.post[title] = "test" it crashes, cause of

undefined method `name' for nil:NilClass (NoMethodError)

I think (please correct me), it's because of the input name, cause it is post[title] instead of only post right? How can I fix it?

Was it helpful?

Solution

How about

target_form.field_with(:name => "post[title]").value = "test"
target_form.field_with(:name => "post[description]").value = "test"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top