Ajax - the Rails way!

36 views
Skip to first unread message

Tommy Ng

unread,
May 29, 2013, 9:44:27 PM5/29/13
to rubyonra...@googlegroups.com
I find the Rails online documentation at
"http://guides.rubyonrails.org/form_helpers.html#dealing-with-ajax"
difficult to follow, due to the way the information is organized. For
example, I wanted to look for Ajax, so I looked under "View". But,
because the material is organized in the MVC format, I can always see
half of the solution, and not an entire one. I don't know if anyone
experienced the same thing.

Back to Ajax, under View, I can see how the form is formed. But, what
is missing is what happened after the form is submitted. Ajax is about
interacting between client side and server side. I realized that I had
to look under "Controller" for the other half of the solution,
especially how Rails renders the result before going back to the client
side. Long story short, here I have a simple form:

<% form_tag({:controller => "main", :action => "contact"}, :remote =>
true, :update => "result") do %>
Name:<br/><%= text_field_tag(:name, :"", :id => "name") %><br/><br/>
Message:<br/><%= text_area_tag(:message, :"", :id => "message")
%><br/>
<%= submit_tag "Send" %>
<% end %>
<div id="result"></div>

First problem, my form now disappeared. It appeared when it was

<%= form_tag("/main/contact", :method => "post", :id => "contactForm")
do %>

What's wrong with the above code ?

Actually, what I really wanted to ask is more on the server side for
this code to work:

def contact
...
end

Anyway, one problem at a time. Thanks so much.

--
Posted via http://www.ruby-forum.com/.

Norbert Melzer

unread,
May 30, 2013, 12:43:42 AM5/30/13
to Rails Mailinglist



2013/5/30 Tommy Ng <li...@ruby-forum.com>


<% form_tag({:controller => "main", :action => "contact"}, :remote =>

There should be an equal sign (=) just after the percent to tell erb, that it should output something
 
true, :update => "result") do %>
   Name:<br/><%= text_field_tag(:name, :"", :id => "name") %><br/><br/>
   Message:<br/><%= text_area_tag(:message, :"", :id => "message")
%><br/>
   <%= submit_tag "Send" %>
<% end %>
<div id="result"></div>

First problem, my form now disappeared.  It appeared when it was 
<%= form_tag("/main/contact", :method => "post", :id => "contactForm")
do %>

Here you have that equal sign, so erb knows to output the formtag, in the line without it, the returnvalue of form_tag is just ommited.
 
Also there are some nice railscasts about Ajax and a blogpost wich kickstarted me a few weeks ago: http://www.alfajango.com/blog/rails-3-remote-links-and-forms/

Tommy Ng

unread,
May 30, 2013, 10:53:29 PM5/30/13
to rubyonra...@googlegroups.com
Thanks Norbert! I read the document you recommended and here's my
opinion:

I found this document pretty good. However, there are a few things I'm
not clear. Under the "Putting it all together" section, the author said
"enough explanation, let's create a remote form that loads some content
into the page,..." but there was not any form at all. Down below, I saw
the "View" section which is totally empty. Then I saw the "Controller"
section which talked about view, namely the "_show.html.erb". I'm very
confused! I think the Comment example that the author tried to present
is pretty good. But it would have been better if he would begin with a
form, then the rails.js, along with any additional javascript, and
finally how Ajax result is rendered in the controller, and formatted
into JSON. Anyway, for the past four days devoting to Ajax on Rails, I
have not seen a complete working Ajax example from anywhere, so
frustrated right now.
Reply all
Reply to author
Forward
0 new messages