开发者

How can i get jQuery to update an element on a page after submitting a form with AJAX on a ruby-on-rails app?

I am trying to use jQuery with Rails and AJAX to accept a post from a user, and update the relevant elements on the page to display the new post.

I have successfully set a post model and controller in my app, and adapted it to accept form submissions with AJAX. I know that the AJAX form submission is working, but I can't get any elements on my page to reload. I have a Post model, Posts controller, a _post_form.html.erb partial that has a html form for posting, and the relevant create.js.erb file.

The create.js.erb file current contains (without the what_to_put_here):

 alert('ajax works!');
 $('#user_info').what_to_put_here?

When it contains just the alert, the file works and a dialogue comes up displaying the message. So, I am certain everything works. However, i just can't figure out what code to user to update the user_info element and the posts table. I am using jQuery and jQuery UI for my javascripts.

So, how do I write in the create.js.erb file a way to update the posts table and the user_info current_user.posts.count?

The following is the show.html.erb file:

 <section>
 <%= render 'shared/discussion_overview' %>
 <% unless @posts.nil? %>
开发者_JAVA技巧 <h3> Thoughts and Opinions </h3>
 <table class= "posts">
         <%= render @posts %>
 </table>
 <%= will_paginate(@posts) %>
 <% end %>
 <%= render 'posts/post_form' %>
 </section>

The posts partial _post.html.erb:

 <tr>
<td >
    <span class="post_header"><h4><%= link_to "#{post.user.first_name} #{post.user.last_name}", post.user %></h4></span>
    <p> <%= post.content %> </p>
</td>
 </tr>

And the _user_info.html.erb partial (which is referenced from application.html.erb) :

 <div id="user_info">
<h1><a href = "<%= user_path(current_user) %>" >
<%= "#{current_user.first_name} #{current_user.last_name}" %>
</a></h1>
<%= pluralize(current_user.posts.count, "post") %> <br>
<%= pluralize(current_user.discussions.count, "discussion")%> <br>

Joined <%= time_ago_in_words(current_user.created_at)%> ago.
 </div>


$('#user_info').html("<%= escape_javascript(render :partial => 'user_info')%>");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜