Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在rails中的控制器内发出get/post请求_Ruby On Rails_Http_Controller - Fatal编程技术网

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

Ruby on rails 在rails中的控制器内发出get/post请求,ruby-on-rails,http,controller,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 b

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

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

你为什么要这么做?只需创建并保存它。如果你有关于这个保存的业务逻辑,那么就创建一个类/上下文/任何东西,并通过它进行保存,除非它足够小,它可以放入模型本身。我发现如果我们可以通过现有的路径保存到控制器,你可以向自己的应用程序发出请求,这将是一件有趣的事情。在这种情况下,我没有很好的理由来添加数据。是的,但我尝试了以下方法来添加数据:post_url=”“address=URI(URI.encode(“#{post_url}”))http=Net::http.new address.host,address.port request=Net::http::Get.new address.request_URI http.start这是很长的路要走;为什么不使用一个更简单的gem来封装所有的工作呢?同样,这似乎是个坏主意。如果没有别的,我会把所有的工作转移到别的地方,这样主线控制器代码就不会被无关的东西污染。
class SavePagesController < ApplicationController
 def save
  Page.create(email: params[:email], name: params[:name])      
 end
end
  match 'add', to: 'build_pages#add', via: [:get,:post]
  match "save" => "save_pages#save", via: [:get,:post]