2

在 DB 中的 Rails 应用程序中,我有一些查询。

# PostController
@post.query # => 'blabla'

我想把这个字符串放在谷歌或雅虎

在我使用的简单红宝石文件中

require ... all gems
Capybara.app_host = 'http://www.google.com/'

class Google
  include Capybara::DSL
  def search
    visit('/')
    fill_in "q", :with => "blah blah" 
    click_button("Google")
    ...

但是我怎样才能把这段代码放在控制器(或模型方法)中?

4

2 回答 2

0

Capybara 不适合此类用途。您可能想尝试机械化

于 2012-11-30T21:02:04.603 回答
0
class PostsController < ApplicationController

require 'capybara'
require 'capybara/dsl'

include Capybara::DSL

Capybara.current_driver = :webkit

  def index
    @posts = Post.all
  end

  def show
    @post = Post.find(params[:id])
     visit(@post.search_type)
     fill_in "q", :with => @post.query 
     click_button("Google")

     loop_variable = 0
     num_of_page = 1
     max_count_of_page = 4
     flag_on_break = false

     while(loop_variable == 0) do
       has_on_page = page.has_content?(@post.search_question)

       if has_on_page
         loop_variable = 1
       else
         find('.b:last a').click
         num_of_page += 1
       end
         if num_of_page > max_count_of_page
           flag_on_break = true
           break
           end
         end

      if flag_on_break
        @number = 'Nothing not found'
      else
        @number = num_of_page
      end  
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(params[:post])

    if @post.save
      flash[:notice] = "Post created!"
      redirect_to root_path
    end
  end
end

这是我的一段代码-但我认为主要思想是可以理解的

于 2012-11-30T21:54:42.083 回答