Ruby on Rails Tuesday, May 28, 2013

Tommy Ng wrote in post #1110326:
> Hello everyone, I'm a newbie at Ruby on Rails. I spent nearly two days
> of the Memorial Day weekend stumbling upon making Ajax working in Rails.
> At this point, I'm so exhausted and hope that I can get some help to
> move forward. Initially, I tried the Ajax approach offered by Rails but
> did not work, so I turned to jQuery. Basically, I want to submit a
> simple form as follows:
>
> <%= form_tag("/main/contact", :method => "post", :id => "contactForm")
> do %>
> Name: <%= text_field_tag(:name) %><br/>
> Message: <%= text_area_tag(:message) %>
> <%= submit_tag "Send" %>
> <% end %>
> <div id="result"></div>
>
>
> And here's my Ajax code in jQuery:
>
> $("#contactForm").submit(function(event) {
>
> /* stop form from submitting normally */
> event.preventDefault();
>
> /* clear result div */
> $("#result").html('');
>
> /* get values from elements on the page: */
> var values = $(this).serialize();
>
> /* Send the data using post and put the results in a div */
> $.ajax({
> url: "/main/contact",
> type: "post",
> data: values,
> success: function(){
> $("#result").html(response.name + "; " + response.message);
> },
> error:function(){
> $("#result").html('there is error while submit');
> }
> });
> });
>
>
> And here's my main controller:
>
> def contact
> name = params[:name]
> message = params[:message]
> respond_to do |format|
> format.html # contact.html.erb
> format.json {
> render :response => {:name => name, :message => message}
> }
> end
> end
>
>
> I just want to test to see if user's name and message can be submitted
> successfully via Ajax, and if so, insert the submitted values (name &
> message) in the <div id="result"></div>. Would you please look through
> and let me know where I did wrong. I appreciate any help. Thanks a
> lot.

You're not saying what the problem is. Is the request not getting to
server? Is the form data not being sent? Is the server not responding?
Where exactly are you failing?

Right off the bat i can see a problem here:
>$.ajax({
> url: "/main/contact",
> type: "post",
> data: values,
> success: function(){
> $("#result").html(response.name + "; " + response.message);
> },
> error:function(){
> $("#result").html('there is error while submit');
> }
> });

Your success callback function needs the response parameter. You're
trying to use it, but the variable is not available as a parameter. It
should look like this:

success: function(data, textStatus, jqXHR){
.....do stuff with 'data'
}

data is the json that's come back from server. jqXHR is something
similar to the full XHR response object.

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/493d74a810a78a1661bd621188666f87%40ruby-forum.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment