只是为了好玩,我写了一个非常小的 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 对吗?我该如何解决?