Ruby:使用Mechanize访问我的表单的问题!

时间:2010-03-11 09:56:08

标签: ruby mechanize

为了好玩,我写了一个非常小的铁杆博客(只是一个问候世界)。 现在我想使用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)

我认为(请纠正我),这是因为输入名称,因为它是帖子[标题]而不是仅发布权利? 我该如何解决?

1 个答案:

答案 0 :(得分:5)

怎么样

target_form.field_with(:name => "post[title]").value = "test"
target_form.field_with(:name => "post[description]").value = "test"
相关问题