我有一个小的PHP服务,由AJAX在JavaScript文件中调用:
$.ajax({ type: "GET", url: "getDate.php", dataType:"json", data :{ fromDate:fromDate, toDate:toDate }, success: function(data) { ...... } });该服务包含:
$fromDate = $_GET['fromDate']; $toDate = $_GET['toDate']; $fromDate=date_create($fromDate); $fromdate = date_format($fromDate,"Y-m-d")."T".date_format($fromDate,"H:i:s")."Z"; $fromdate = urlencode($fromdate); $toDate=date_create($toDate); $todate = date_format($toDate,"Y-m-d")."T23:00:00Z"; $todate = urlencode($todate); $url = "http://194.209.53.19:8086/query?db=Bellevue&q=select+*+from+measures+where+time%3E%3D%27".$fromdate."%27+and+time%3C%3D%27".$todate."%27"; $data = file_get_contents($url, false); echo $data;我需要在我的Rails应用程序中使用它。 我想知道我是否可以将.php文件放在Rails文件夹中,然后简单地调用它。 或者,如果有办法在Rails中执行类似的服务?
I have a small PHP service that is being called in a JavaScript file by AJAX :
$.ajax({ type: "GET", url: "getDate.php", dataType:"json", data :{ fromDate:fromDate, toDate:toDate }, success: function(data) { ...... } });This service contains :
$fromDate = $_GET['fromDate']; $toDate = $_GET['toDate']; $fromDate=date_create($fromDate); $fromdate = date_format($fromDate,"Y-m-d")."T".date_format($fromDate,"H:i:s")."Z"; $fromdate = urlencode($fromdate); $toDate=date_create($toDate); $todate = date_format($toDate,"Y-m-d")."T23:00:00Z"; $todate = urlencode($todate); $url = "http://194.209.53.19:8086/query?db=Bellevue&q=select+*+from+measures+where+time%3E%3D%27".$fromdate."%27+and+time%3C%3D%27".$todate."%27"; $data = file_get_contents($url, false); echo $data;I need to use this in my Rails application. I was wondering if I could put the .php file in a Rails folder, and simply call it. Or if there's a way to do a similar service in Rails?
最满意答案
当然,你可以在Rails中做到这一点。 首先,您需要为您的请求定义路线。 在routes.rb文件中写下以下内容,位于config / routes.rb:
get "/getDate" => "generals#get_date"这意味着:每当有/getDate的GET请求时,转到控制器GeneralController ,并调用其名为get_date函数。 如果你写了users#get_date ,Rails会尝试在UsersController找到get_date方法。
通过以下命令生成您选择的控制器:
rails generate controller General它将在app / controller和其他文件中生成一个名为general_controller.rb的文件。
现在,您需要在那里定义所有逻辑。 其他的事情:在PHP中,您使用$_GET['formDate']获取发送的数据,但在Rails中,您将通过执行params[:formDate]来完成相同的功能。
def get_date form_date = params[:formDate] # and now here you can write all your logic, and can finally return the response respond_to do |format| format.json { render json: data_to_be_returned } end end编辑:
对于PHP中的date_create或date_format函数,您可以在此处和此处查找它们的等价物。
Sure, you can do this in Rails. First, you need to define the route for your request. Write the following in your routes.rb file, located at config/routes.rb:
get "/getDate" => "generals#get_date"What that means is: whenever there is a GET request for /getDate, go to controller GeneralController, and invoke its function named get_date. If you wrote users#get_date, Rails would try to find the get_date method in UsersController.
Generate the controller of your choice by following command:
rails generate controller GeneralIt will generate a file named general_controller.rb in app/controller among other files.
Now, you need to define all your logic there. Other thing: in PHP, you get the sent data using $_GET['formDate'], but in Rails, you will accomplish the same functionality by doing params[:formDate].
def get_date form_date = params[:formDate] # and now here you can write all your logic, and can finally return the response respond_to do |format| format.json { render json: data_to_be_returned } end endEdit:
For date_create or date_format functions in PHP, you can look up their equivalents at here and here.
更多推荐
发布评论