I have a functionality of posting messages to a group which works using AJAX via Jquery. When I click on the "Post" button which is the value of the submit_tag, javascript is rendered and the page without reloading displays the latest message posted. Basically the post_message.js.erb file is called. I am making use of the following tutorial to implement the same:- http://railscasts.com/episodes/136-jquery
The code for post_message.js.erb looks like this:-
$("#new_post").before('<div id ="new_post"><%= escape_javascript(flash.delete(:notice)) %></div>');<!--TO-DO the flash messages aren't yet enabled/working-->
$("#latest_post").prepend("<%= @group_post.message %> by <%= Investor.find(@group_post.post_by).first_name %> <div class=contentdispgrp> <%= distance_of_time_in_words(@group_post.created_at,Time.now) %> ago</div> <br/><br/><hr/>");
$("#post_msg")[0].reset();
The part of the form which would otherwise render HTML and display the posted message with reloading the page looks like this:-
<%form_for :group_post, @group_post, :url => {:action => :post_message, :id => params[:id]},:html => {:multipart => 'true', :id => 'new_post'},:id => 'post_form' do |f| -%>
Start Discussion:<label id="post_msg"><%=f.text_field :message%></label>
<!--<%=f.file_field :photo%> -->
<%=submit_tag "Post" %></p>
<!--<input type="button" value="Reset Form" onClick="this.form.reset()" />-->
<%end%>
<div id = "latest_post"> </div>
<%for a in @group_all_posts %>
<%= a.message %> by <%= Investor.find(a.post_by).first_name %> <div class ="contentdispgrp" id="style_chck"> <%= distance_of_time_in_words(a.created_at,Time.now) %> ago </div><br/><br/> <hr/>
<%end%>
</tr>
I want to empty the text field also along with the above already implemented functionality. I guess , I need to some how make use of the Javascript reset
method , but somewhere I am not using it correctly.
Kindly help me with the same.
Thanks for your help..
[EDIT]
Code for post_message in my groups controller.
def post_message
@investor_group = InvestorGroup.find(params[:id])
unless @current_user.is_an_existing_member_of_group(@investor_group)
flash[:notice] = "Please join this group to participate in discussions"
redirect_to :action => :show, :id => @investor_group and return # try to find out what exactly does this command do with return stmnt
else
@group_post = GroupPost.new(params[:group_post])
end
#@group_post = GroupPost.new(params[:group_post])
investor_id = session['investor_id']
@group_post.investor_group_id = @investor_group.id
@group_post.post_by = investor_id
if @group_post.message.blank?
flash[:notice] = 'Post can\'t be blank.'
end
if @group_post.save
flash[:notice] = 'Post was successfully created.'
# redirect_to :action => :show, :id => params[:id] - removed after trying to implement ajax via jquery
else
flash[:notice] = 'Post was not successfully created.'
end
respond_to do |format|
format.html {redirect_to :action => "show", :id => params[:id]}
format.js
end
end
I assume you want something like this:
That will clear the message input box value.