Ok, in PHP I can do this in 5 minutes. With Rails I am completely lost. I am so new to Rails and I have a requirement to do more advanced stuff with it then I am comfortable with currently. The Big guy decided lets go Rails from PHP. So its crash course learning. Fun times..
I understand from the javascript side making the post/get with jquery remains the same. On the ruby side however I am completely lost. What do I need to do in order to do a post/get to a Rails App. How would I define my controller? Lets say from the HTML side I am posting
input1 and input2 standard jquery $.post with a JSON request for data back. With php I would make a simple controller like:
public function myFormPostData(){
$errors = "none";
$errorMsg = "";
if($_POST['input1'] == ""){$errors = "found"; $errorMsg .= "Input 1 Empty<br>";}
if($_POST['input2'] == ""){$errors = "found"; $errorMsg .= "Input 2 Empty<br>";}
if($errors == "found"){ $output = array("error" => "found", "msg" => $errorMsg); }
else{
$output = array("error" => "none", "msg" => "Form Posted OK!!");
}
echo json_encode($output);
}
but in ruby I dunno how to translate that concept. I’m open to suggestions and good practices. Grant it the above is a super mediocre rendition of what I am needing to do I would sanitize things more than that and all else, but it gives the idea of what I am looking for in Rails..
First of all, Rails 3 has a nice feature called
respond_with. You might check out this Railscast episode that talks about it. http://railscasts.com/episodes/224-controllers-in-rails-3. The examples he uses are great if you are dealing with resources.Having said that, I’ll give you an example with a more traditional approach. You need to have a controller action that handles requests which accept JSON responses.
Make sure that you have a route set up
Those are the basics that you will need. Rails gives you a lot for free, so you may find that much of what you are looking for already exists.
If you post a more concrete example of what you are trying to you might get even better advice. Good luck!