doskmc7870 2015-06-27 00:12
浏览 23


I have a small PHP service that is being called in a JavaScript file by AJAX :

    type: "GET",
    url: "getDate.php",                 
    data :{
    success: function(data) {

This service contains :

$fromDate = $_GET['fromDate'];
$toDate = $_GET['toDate'];
$fromdate = date_format($fromDate,"Y-m-d")."T".date_format($fromDate,"H:i:s")."Z";
$fromdate = urlencode($fromdate);
$todate = date_format($toDate,"Y-m-d")."T23:00:00Z";
$todate = urlencode($todate);
$url = "*+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?

  • 写回答

1条回答 默认 最新

  • dqsa17330 2015-06-27 00:50

    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 General

    It 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 } 


    For date_create or date_format functions in PHP, you can look up their equivalents at here and here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥100 webapi的部署(标签-服务器)
    • ¥20 怎么加快手机软件内部计时的时间(关键词-日期时间)
    • ¥15 C语言除0问题的检测方法
    • ¥15 为什么四分管的内径有的是16mm有的15mm,四分不应该是12.7mm吗
    • ¥15 macos13下 ios交叉编译的问题
    • ¥15 bgz压缩文件怎么打开
    • ¥15 封装dll(引入了pcl的点云设计库)
    • ¥30 关于#开发语言#的问题:我需要在抄板的基础上再抄板抄程序,根据RDA8851CM基础上开发
    • ¥15 oracle 多个括号,怎么删除指定的括号及里面的内容?
    • ¥15 小新14API2019想用bios调风扇