只是为了好玩,我写了一个非常小的 Rails 博客(只是一个 hello world)。 现在我想使用 mechanize 创建一个帖子。 所以我创建了一个 Ruby Prog 并开始编码。
这是我的问题: Rails 创建我的表单元素,包括所有输入。 在 HTML 中,我的输入如下所示:
<input type="text" size="30" name="post[title]" id="post_title">
或
<textarea rows="20" name="post[description]" id="post_description" cols="40"></textarea>
嗯... 这是我使用 Mechanize 的 Ruby Prog:
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"
我知道我的错误在哪里,但我不知道如何修复它。 在 target_form.post[title] = "test"处崩溃,原因为
undefined method `name' for nil:NilClass (NoMethodError)
我想(请纠正我),这是因为输入的名称,因为它是post[title]而不是仅仅post,对吧? 我该如何修复它?
最佳答案
怎么样
target_form.field_with(:name => "post[title]").value = "test"
target_form.field_with(:name => "post[description]").value = "test"
关于Ruby:使用 Mechanize 访问我的表单时出现问题!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2423971/