1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Can everything be done in Ruby on Rails?

Discussion in 'Ruby' started by Kalyse, May 17, 2008.

  1. #1
    Hello,
    I want to know (before I start learning Ruby) can everything be done ROR that can be done in PHP?

    I have already started a very large object orientated modular/scalable website that uses a lot of SERVER variables to create many sites from a single script/database.

    My question is, now that I've actually finished core code, I am moving onto design and have been told that Ajax + RoR complement each other perfectly.
    SEMrush
    I have no qualms about restarting the several weeks of work i've already done because I'm sure I can improve on things, but can RoR handle anything that PHP can?
     
    Kalyse, May 17, 2008 IP
    SEMrush
  2. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #2
    I'm not sure about RoR's overall possibilities, but I suggest you stick with php, there is so much more support for php, and it is open source which makes it even better
     
    crath, May 17, 2008 IP
  3. eLDee

    eLDee Peon

    Messages:
    131
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    RoR is a framework based on Ruby.. PHP is not a framework, its a programming language like Ruby..

    Ajax is another ballgame.. its not that easy to work with.. And even frustrating at time.. The browser incompatibility can drive you insane.

    I chose to only support firefox on my new project. But you may not have that luxury
     
    eLDee, May 17, 2008 IP
  4. mrphp

    mrphp Well-Known Member

    Messages:
    680
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #4
    PHP is a programming language but there's a lot of framework you can use like zend, cakephp, codeigniter, etc etc.

    Ajax is cool and its not really frustrating unless you dont have interest.

    Not all clients use firefox, so my recommendation is check the compatibility as IE still dominates.
     
    mrphp, May 18, 2008 IP
  5. TeraTask

    TeraTask Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I use this Javascript code to POST and GET things to the server and it works fine on all the major browsers. Hopefully it helps you.

    
    var request = false;
    var requested_page = null;
    
    function ajaxSend(url,data,form_type) {
      if (form_type.length == 0) {
        form_type = "GET";
      }
      try {
        request = new XMLHttpRequest();
      } catch (trymicrosoft) {
        try {
          request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (othermicrosoft) {
          try {
            request = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (failed) {
            request = false;
          }
        }
      }
    
      if (!request) {
        //Error
      } else {
        //ajax commands available.
    
        if (form_type == "POST") {
          request.open(form_type, url, true);
          request.onreadystatechange = ajaxReceive;
          request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
          request.send(data+"&ajax_call=true");
        } else {
          request.open(form_type, url+"?"+data+"&ajax_call=true", true);
          request.onreadystatechange = ajaxReceive;
          request.send(null);
        }
        requested_page = data;
      }
    }
    function ajaxReceive() {
      if (request.readyState == 4) {
        if (request.status == 200) { //everything went smoothly
          var response = request.responseText;
          receivePage(response);
        }
        // else an error accessing the URL.
      }
      // else still processing, so do not execute routine
    }
    
    PHP:
    To send data to the server, use the ajaxSend(url,data,form_type) function. The result will be returned as the first parameter to a javascript function you must write and which must be called receivePage(response).

    To use JSON for returning variables, I create an array called $ajax_return of the form $ajax_return[$key] = $value and then run it through this:

    
    $ajax_return = array("action"      =>"", //either "update node" or "call function"
                         "targetNode"  =>"",
                         "newContent"  =>"",
                         "className"   =>"",
                         "callFunction"=>"false",
                         "functionName"=>""
                         
                        );
    $echo_message = ""; $prefix = "";
    foreach ($ajax_return as $key=>$value) {
      $echo_message .= $prefix.'"'.$key.'":"'.str_replace("\r",'',str_replace("\n",'\n',str_replace('"','\"',$value))).'"';
      $prefix = ",";
    }
    echo '{'.$echo_message.'}';
    
    PHP:
    I then use a generalized receivePage function shown here:
    
    /* Ajax Receive function
     *
     * Assumes JSON return variable.
     * The following variables are defined for this function:
     *
     * action       := "update node" to pass newContent to targetNode and change the targetNode class name to className
     *              := "call function" to call the Javascript function functionName.
     * 
     * targetNode   := The DOM ID for the item to receive newContent's value.
     *
     * newContent   := The content to replace targetNode's content.
     *
     * className    := The class name for targetNode when updating to newContent
     *
     * callFunction := True if functionName is to be called after content is updated and action=="update node"
     *
     * functionName := Name of funciton to be called if action=="call function" or callFunction=="true"
    */
    function receivePage(returnObject) {
      content = eval('('+returnObject+')');
      if (content.action == "update node") {
        //Update Content
        document.getElementById(content.targetNode).innerHTML = content.newContent;
        //Update Class
        if (content.className) {
          document.getElementById(content.targetNode).className = content.className;
        }
      }
      if (content.action == "call function" || content.callFunction == "true") {
        //Used to call custom functions by add on scripts
        //Passes the returnObject as an object to the function named
        window[content.functionName](content);
      }
    }
    
    PHP:
    Yes, it took awhile to put all of that together and test it on various browsers, but it gives great results and is very extendible. I just plug in the Ajax script and then all I have to do is populate the $ajax_return and adjust my Javascript, so it makes it fairly easy to work with Ajax with PHP and doesn't require any "frameworks".

    Hope that helps.

    Oh, one other bit. So that I know for sure if a POST or GET is through Ajax, the functions above will pass a variable called ajax_call with a value of true for each Ajax call, so I can use code like this:
    
    if ($_POST['ajax_call'] == true || $_GET['ajax_call'] == true) {
      $ajax_return = array("action"      =>"", //either "update node" or "call function"
                           "targetNode"  =>"",
                           "newContent"  =>"",
                           "className"   =>"",
                           "callFunction"=>"false",
                           "functionName"=>""
                           
                          );
      $echo_message = ""; $prefix = "";
      foreach ($ajax_return as $key=>$value) {
        $echo_message .= $prefix.'"'.$key.'":"'.str_replace("\r",'',str_replace("\n",'\n',str_replace('"','\"',$value))).'"';
        $prefix = ",";
      }
      echo '{'.$echo_message.'}';
      }
    
    PHP:
     
    TeraTask, May 18, 2008 IP