루비 : Mechanize를 사용하여 내 양식에 액세스하는 데 문제가 있습니다!

StackOverflow https://stackoverflow.com/questions/2423971

  •  19-09-2019
  •  | 
  •  

문제

재미를 위해, 나는 아주 작은 레일 블로그 (단지 Hello World)를 썼습니다. 이제 Mechanize를 사용하여 게시물을 만들고 싶습니다. 그래서 나는 루비 프로그를 만들고 코딩을 시작했습니다.

내 문제는 다음과 같습니다. 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를 사용한 내 루비 프로그가 있습니다.

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"

나는 내 오류가 어디에 있는지 알고 있지만 그것을 고치는 방법을 모른다. at target_form.post [title] = "test"는 충돌, 원인

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

나는 입력 이름 때문이라고 생각한다. 어떻게 고칠 수 있습니까?

도움이 되었습니까?

해결책

어때

target_form.field_with(:name => "post[title]").value = "test"
target_form.field_with(:name => "post[description]").value = "test"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top