通过链接点击向其他网站发送获取请求?

时间:2013-12-30 10:23:53

标签: ruby-on-rails ruby http

我有一个方法:

  def getRequest(link)
    require 'net/http'

    url = URI.parse(@target + link)
    reqest = Net::HTTP::Get.new(url.to_s)
    response = Net::HTTP.start(url.host, url.port) {|http|
          http.request(request)
    }
    console.log(response)
  end

我希望视图中的链接可以调用getRequest("www.google.com"),类似于:

<%= link_to 'Test GET request', 'getRequest/link="www.google.com"%>

我该怎么做?

2 个答案:

答案 0 :(得分:2)

你做不到。了解有关Rails的更多信息。之后尝试这个解决方案:

<%= link_to 'Test GET request', test_users_path(test_link: "www.google.com")%>

UsersController:

def test
    link = params[:test_link] 
    require 'net/http'

    url = URI.parse(@target + link)
    reqest = Net::HTTP::Get.new(url.to_s)
    response = Net::HTTP.start(url.host, url.port) {|http|
          http.request(request)
    }
    console.log(response)
    redirect_to ...
end

答案 1 :(得分:-1)

:target => '_blank'参数应该是link_to的参数,而您将其放在image_tag参数中。像这样修改你的代码:

<%= link_to("google", :alt => "Google"), "http://www.google.com", :target => "_blank" %>