在我的Rails应用程序中我有一些查询。
# PostController
@post.query # => 'blabla'
我想把这个字符串放在谷歌或雅虎
在简单的ruby文件中我使用
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")
...
但是如何将此代码放在控制器(或模型方法)中?
答案 0 :(得分:0)
Capybara不适合此类用途。您可能想尝试mechanize。
答案 1 :(得分: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
这是我的一部分代码 - 但我认为主要的想法是可以理解的