在rails中的控制器内发出get / post请求

时间:2015-06-28 20:41:54

标签: ruby-on-rails http controller

我在控制器中有以下代码作为操作:

class BuildPagesController < ApplicationController
 def add
  email= "abc@yahoo.com"
  name= "abc"
  # I want to make a get request/call 
  # such as localhost:3000/save?email=abc@yahoo.com&name=abc 

 #I tried the following but my console simply freezes
    post_url = "http://localhost:3000/save?email=abc@yahoo.com&name=abc"
    address = URI(URI.encode("#{post_url}"))
    http = Net::HTTP.new address.host, address.port
    request = Net::HTTP::Get.new address.request_uri
    http.start


 end
end

另一个控制器如下:

class SavePagesController < ApplicationController
 def save
  Page.create(email: params[:email], name: params[:name])      
 end
end

routes.rb如下:

  match 'add', to: 'build_pages#add', via: [:get,:post]
  match "save" => "save_pages#save", via: [:get,:post]

0 个答案:

没有答案