Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 2.79 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Ajax functionality seems to fail when on adding a div tag to my .js.erb file
  2. $("#new_post").before('<div id ="new_post"><%= escape_javascript(flash.delete(:notice)) %></div>');<!--TO-DO the flash messages aren't yet enabled/working-->
  3. $("#latest_post").prepend("<%= @group_post.message %> by <%=  Investor.find(@group_post.post_by).first_name %>  <%=  distance_of_time_in_words(@group_post.created_at,Time.now) %> ago <br/><br/><hr/>");
  4. $("#new_post")[0].reset();
  5.        
  6. $("#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/>");
  7.        
  8. <table>
  9. <tr>
  10.  
  11. <%if @current_user.is_an_existing_member_of_group(@investor_group)%>
  12. <%form_for  :group_post, @group_post, :url => {:action => :post_message, :id => params[:id]},:html => {:multipart => 'true', :id => 'new_post'} do |f| -%>
  13.     Start Discussion:<br><%=f.text_field :message%>
  14.    <!--<%=f.file_field :photo%> -->
  15.    <%=submit_tag "Post"%></p>
  16.   <%end%>
  17.  
  18.    <div id = "latest_post"> </div>
  19.  
  20.  
  21. <%for a in @group_all_posts %>
  22. <%= 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/>
  23.  
  24.         <%end%>
  25.  
  26.  
  27. </tr>
  28. <%end%>
  29. </table>
  30.        
  31. def post_message
  32.       @investor_group = InvestorGroup.find(params[:id])
  33.  
  34.  
  35.       unless @current_user.is_an_existing_member_of_group(@investor_group)
  36.         flash[:notice] = "Please join this group to participate in discussions"
  37.         redirect_to :action => :show, :id => @investor_group and return # try to find out what exactly does this command do with return stmnt
  38.       else
  39.         @group_post = GroupPost.new(params[:group_post])
  40.       end
  41.       #@group_post = GroupPost.new(params[:group_post])
  42.  
  43.       investor_id = session['investor_id']
  44.       @group_post.investor_group_id = @investor_group.id
  45.       @group_post.post_by = investor_id
  46.       if @group_post.save
  47.         flash[:notice] = 'Post was successfully created.'
  48.        # redirect_to :action => :show, :id => params[:id] - removed after trying to implement ajax via jquery
  49.       else
  50.         flash[:notice] = 'Post was not successfully created.'
  51.       end
  52.  
  53.  
  54.       respond_to do |format|
  55.         format.html {redirect_to :action => "show", :id => params[:id]}
  56.         format.js
  57.       end
  58.  
  59.   end
  60.        
  61. jQuery.ajaxSetup({
  62.     'beforeSend' : function(xhr) {
  63.         xhr.setRequestHeader("Accept","text/javascript")
  64.         }
  65. })
  66.  
  67. $(document).ready(function(){
  68.     $("#new_post").submit(function(){
  69.         $.post($(this).attr("action"),$(this).serialize(),null,"script");
  70.         return false;
  71.     })
  72. })
  73.        
  74. form_for  :group_post, @group_post, :remote=>true ...
  75.        
  76. format.js do
  77.     puts "I am here"
  78. end