只是为了好玩,我写了一个很小的铁轨博客(只是一个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>

好吧...这是我使用机械化的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)

我认为(请纠正我),这是因为输入名称,因为它是帖子[title]而不是帖子,对吗?我该如何解决?

有帮助吗?

解决方案

怎么样

target_form.field_with(:name => "post[title]").value = "test"
target_form.field_with(:name => "post[description]").value = "test"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top