Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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
WEB从PHP到Ruby_Php_Ruby On Rails_Ruby - Fatal编程技术网

WEB从PHP到Ruby

WEB从PHP到Ruby,php,ruby-on-rails,ruby,Php,Ruby On Rails,Ruby,我需要了解Ruby(ROR)的逻辑如何用于web开发。我已经尝试过Ruby教程,但需要看看简单web系统的代码 你能用PHP重写下面的简单代码吗,就像它写在ROR上一样?你会帮我很多忙的 //HTML/CSS/JS: <html> <head>.. <script> function send(){ var xmlhttp = getXmlHttp(); xmlhttp.open('POST', '/send.php', true); xmlhttp.se

我需要了解Ruby(ROR)的逻辑如何用于web开发。我已经尝试过Ruby教程,但需要看看简单web系统的代码

你能用PHP重写下面的简单代码吗,就像它写在ROR上一样?你会帮我很多忙的

//HTML/CSS/JS:

<html>
<head>..
<script>
function send(){
var xmlhttp = getXmlHttp(); 
xmlhttp.open('POST', '/send.php', true); 
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
xmlhttp.send("data=" + $("#test").val() );              

xmlhttp.onreadystatechange = function() { 
    if (xmlhttp.readyState == 4) 
        if(xmlhttp.status == 200)
           if(xmlhttp.responseText) alert("Done");
}
</script>
</head>

<body>
<input type='text' id='test' />
<button onclick='send()'>OK</button>
</body>

</html>

..
函数send(){
var xmlhttp=getXmlHttp();
open('POST','/send.php',true);
setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlhttp.send(“data=“+$(“#test”).val());
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4)
if(xmlhttp.status==200)
if(xmlhttp.responseText)警报(“完成”);
}
好啊
//Send.php

<?php>

$data=$_POST['data'];

if($data=='1')
  echo true;
else
  echo false;

</php>
首先,请尝试了解RubyonRails(!=Ruby!!)是如何工作的

在您的情况下,此代码将有意义:

# config/routes.rb
Rails.application.routes.draw do
  post 'example' => 'example#something'

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
您的控制器:

# app/controllers/example_controller.rb
class ExampleController < ApplicationController
  def something
    result = { ok: params[:data] == "1" }
    render json: result
  end
end
#app/controllers/example_controller.rb
类ExampleController<应用程序控制器
定义某事
结果={ok:params[:data]=“1”}
呈现json:result
结束
结束

因此,您可以发布到
/example
并获得一个json对象:
{“ok”:}

RubyonRails是一个用Ruby编写的非常高效的web应用程序框架。因此,您需要学习RubyonRails框架,除非您无法了解所有内容背后的逻辑。您应该从阅读有关Ruby的教程开始。(顺便说一句,
echo-true
将输出
1
echo-false
将不会输出任何东西)。@Vishal-在学习Ruby-on-Rails之前,他应该真正学习Ruby语言。否则就像说你应该先学习jQuery再学习香草Javascript一样。@Magnueriksson是的。。你是对的,兄弟。。我的错误:)只要为我粉刷一下我的房子,它会帮助很多人了解它的位置/示例以及它的样子?什么?请阅读我在答案中链接的指南,我认为你不了解整个路由。好的,我会以另一种方式问。示例控制器是否像PHP代码一样工作,从客户端接收数据并响应?是的。(这就是调度)总之,在这个例子中,服务器端是controller.rb?routes.rb呢?它看起来像Ajax。它在哪里?在我的例子中,Ajax从HTML文件发送数据到PHP。