At the Forge - RJS Templates
Listing 2. index.rhtml (Ajax Version)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title id="title">Sample HTML page</title>
<%= javascript_include_tag 'prototype' %>
</head>
<body>
<h1 id="headline">Headline</h1>
<form id="theForm">
<p><input type="text" name="future_headline" /></p>
</form>
<p><%= submit_to_remote "submit-button",
"Pig Latin it!",
:url => { :action => "piglatin_sentence" },
:submit => "fakeForm",
:update => "headline" %></p>
</body>
</html>
We also have replaced our button with a call to the submit_to_remote helper:
<p><%= submit_to_remote "submit-button",
"Pig Latin it!",
:url => { :action => "piglatin_sentence" },
:submit => "fakeForm",
:update => "headline" %></p>
The above code does quite a few things:
It creates a button, whose DOM ID is submit-button.
The button has a label of “Pig Latin it!”.
When the button is clicked, it uses Ajax to invoke the piglatin_sentence action on the server, within the current controller.
The contents of the form with the ID fakeForm are submitted.
The value returned from the Ajax invocation is used to update the contents of the HTML element with the ID headline.
All that's left for us to examine is our controller, shown in Listing 3. The controller doesn't necessarily know that it is being invoked by a background Ajax process or that its contents will be used to update the headline element. Rather, it simply is invoked like any method, turning the words into Pig Latin. The translated sentence is returned to the user's browser as a plain-text file.
Listing 3. showme_controller.rb
class ShowmeController < ApplicationController
def piglatin_sentence
# Get the headline
sentence = params[:future_headline]
words = sentence.split
sentence = ""
# Go through each word, applying the secret
# Pig Latin algorithm
words.each do |word|
if word =~ /^[aeiou]/i
word << "way"
else
first_letter = word.slice(0,1)
rest = word.slice(1..-1)
word = "#{rest}#{first_letter}ay"
end
sentence << word
sentence << " "
end
render :text => sentence
end
end
Now, the real magic begins. As we just saw, our controller (piglatin_sentence) returns a plain-text document to its caller. Of course, we're free to return data in whatever format we please. One possible format might be XML. Indeed, the term Ajax is supposed to stand for Asynchronous JavaScript and XML, so it should come as no surprise that XML is a common format for return values. Another format that is growing in popularity is JSON (JavaScript Object Notation), a textual version of JavaScript objects that makes it fast and easy to exchange data.
But, in the world of Ruby on Rails, there is another type of data that the controller might return to the user's browser, namely JavaScript. This might not sound all that clever, but consider what Prototype does with it. If a controller is invoked with link_to_remote or submit_to_remote, and if the HTTP response has a content-type of xml+javascript, the JavaScript is evaluated.
This could potentially save time—instead of returning the text that should be used for the headline and then using JavaScript to insert it. (True, we were able to say this tersely by using an :update parameter to the call to submit_to_remote. But the code still exists.) Rather, we simply could return JavaScript code that uses the DOM to modify the document. This would simplify our code quite a bit.
Listing 4. index.rhtml (Updated for JavaScript in the HTTP Response)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title id="title">Sample HTML page</title>
<%= javascript_include_tag 'prototype' %>
</head>
<body>
<form id="fakeForm">
<h1 id="headline">Headline</h1>
<p><input type="text" name="future_headline" /></p>
</form>
<p><%= submit_to_remote "submit-button",
"Pig Latin it!",
:url => { :action => "piglatin_sentence" },
:submit => "fakeForm" %></p>
</body>
</html>
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Speed Up Your Web Site with Varnish | Jun 19, 2013 |
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
- Speed Up Your Web Site with Varnish
- Containers—Not Virtual Machines—Are the Future Cloud
- Linux Systems Administrator
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Senior Perl Developer
- Technical Support Rep
- Non-Linux FOSS: libnotify, OS X Style
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- RSS Feeds
- excellent
14 min 49 sec ago - good point!
17 min 40 sec ago - Varnish works!
26 min 47 sec ago - Reply to comment | Linux Journal
56 min 24 sec ago - Reply to comment | Linux Journal
3 hours 22 min ago - Reply to comment | Linux Journal
7 hours 22 min ago - Yeah, user namespaces are
8 hours 38 min ago - Cari Uang
12 hours 9 min ago - user namespaces
15 hours 3 min ago - yea
15 hours 29 min ago
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
listing2 index.rhtml
I think you have to send the correct form id in the submit_to_remote
:submit => "theForm"in place of:submit => "fakeForm"(or changing the form id to fakeForm)