Hello all,
I am trying to render via javascript a post and an image in my rails app. My app has the config of Ruby 1.8.7 and Rails 2.0.2 .
With great difficulty I have found a suitable version of paperclip( https://github.com/thoughtbot/paperclip/commit/d177c8f838458348a9f4d1fe41918c5bd99a989c ) to meet the above config requirements.
Earlier before integrating image upload feature in my app for each post, post message where being rendered via javascript using AJAX with Jquery 1.4 . This stopped working when I had try to implement uploading images in my app using paperclip... I request you to please look into this for details of the same:- http://stackoverflow.com/questions/6070510/unable-to-figure-out-why-a-photo-attribute-of-the-paperclip-plugin-is-not-being-p .
From this experience I have been able to in a way infer I need to find an appropriate paperclip version suiting my requirements... unless.. they could be other ways to deal with this situation.. If so.. It would be great if you could enlighten me on the same...
Going forward I have the following options with me:-
1. Find a suitable paperclip plugin which would have support for AJAX using Jquery. (I am really not too sure on how I would be doing this..)
2. Try using scriptaculous and/or prototype( I am unsure on whether I need to use either or both of them ). The concern with this is , I have to implement this from the scratch and I have to figure out from scratch how to go about the same and also the fact that Ajax using Jquery already works for me with post and not with post + Image.
I would like to add some details(code, other points) wrt option 1 wrt the problem I am currently facing:-
I have used option 1 with the help of the Railscast by Ryan Bates(#136 jQuery - RailsCasts).
I have made appropriate modifications referring to the same.. The file that I am trying to render via JS is 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 completely for now.. --> <!--$("#latest_post").before('<div id ="latest_post"><%= escape_javascript(flash.delete(:notice)) %></div>');--> $("#latest_post").prepend("<br/> <%= image_tag @group_post.avatar.url(:thumb) %> <%= @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/>"); $("#latest_post").append("<br/> <%for a in @group_all_posts %> <%= image_tag a.avatar.url(:thumb) %> <%= a.message %> by <%= Investor.find(a.post_by).first_name %> <div class=contentdispgrp> <%= distance_of_time_in_words(a.created_at,Time.now) %> ago</div> <br/><br/><hr/><%end%>"); $('#post_msg input').val('');
I am very much a novice with Jquery and hardly have around 3-4 months of experience with RoR, I have one doubt over here if one could help me clarify..If you have read http://stackoverflow.com/questions/6070510/unable-to-figure-out-why-a-photo-attribute-of-the-paperclip-plugin-is-not-being-p , you would observe by just removing the `:id => 'new_post'` from within the `:html` symbol stopped the .js.erb from being rendered..
My doubt is I am pretty much unclear and lack knowledge on exactly what role is the id "new_post" playing.. when I have actually commented the same.. in my post_message.js.erb.. It might be that the id is used to uniquely identlify the HTML partial that is being rendered.. in which I currently have the part of the code taking the parameters of the image and the message along with it... Not too sure.. Or is there any other side to it ...If so kindly widen my horizon on the same..
The part of the file(this is a partial) which otherwise gets rendered via HTML has the following code in it:-
<%form_for :group_post, @group_post, :url => {:action => :post_message, :id => params[:id]},:html => {:multipart => true}, :id => 'new_post' do |f| %> Start Discussion:<%=f.text_field :message%> <%= f.file_field :avatar %> <%=f.submit "Post" %> <%end%>
<div id = "latest_post"> </div> <%for a in @group_all_posts %> <%if !a.avatar_file_name.to_s.empty? %> <br/><%= image_tag a.avatar.url(:thumb) %> <%end%> <br/><%= a.message %> <br/> 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/>
My post_message method in my controller looks like this:-
def post_message @group_post = GroupPost.new(params[:group_post]) #@group_comment = GroupComment.new(params[:group_comment])
@investor_group = InvestorGroup.find(params[:id]) @group_post.investor_group_id = @investor_group.id investor_id = session['investor_id'] @group_post.post_by = investor_id if !@group_post.message.blank? @group_post.save flash[:notice] = 'Post was successfully created.' else flash[:notice] = 'Post was not successfully created. Post can\'t be blank' end # @group_comment.comment_by = investor_id # @group_comment.group_post_id = :post_id