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 .
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 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..
-
-
- $("#latest_post").prepend("
<%= image_tag @group_post.avatar.url(:thumb) %> <%= @group_post.message %> by <%= Investor.find(@group_post.post_by).first_name %> <%= distance_of_time_in_words(@group_post.created_at,Time.now) %> ago
"); - $("#latest_post").append("
<%for a in @group_all_posts %> <%= image_tag a.avatar.url(:thumb) %> <%= a.message %> by <%= Investor.find(a.post_by).first_name %> <%= distance_of_time_in_words(a.created_at,Time.now) %> ago
<%end%>"); - $('#post_msg input').val('');
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%>
-
-
- <%for a in @group_all_posts %>
- <%if !a.avatar_file_name.to_s.empty? %>
-
<%= image_tag a.avatar.url(:thumb) %> - <%end%>
-
<%= a.message %>
by <%= Investor.find(a.post_by).first_name %> <%= distance_of_time_in_words(a.created_at,Time.now) %> ago
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
- #
- # if @group_comment.save
- # flash[:notice] = 'Comment was successfully created.'
- # else
- # flash[:notice] = 'Comment was not successfully created.'
- # end
-
- @group_all_posts = GroupPost.find(:all, :conditions => [ 'investor_group_id = ?', "#{@investor_group.id}" ], :order => 'created_at DESC')
-
- respond_to do |format|
- format.html {redirect_to :action => "show", :id => params[:id]}
- format.js
- end
-
- end
Kindly let me know if in case my providing any other information could be of any help to answer my query..
Any suggestion on using either option 1 or 2 with where can I find appropriate support for the same( for e.g. the version of the paperclip that I could use in case I am go with option 1 or appropriate tutorials I could make use of and what files I would need(like the jquery lib file in case 1) in case of option 2) would be really of great use to me.
Kindly suggest me something that I can complete at the earliest without too much effort (I have less than a day with me to get this working..).. In the case if I would be actually able to get it working..
**[Update in ORIGINAL QUESTION]**
I am sorry, I have to mention this change.. I need to make use of Jquery alone to get this working for me....
I would like to add Paperclip and Jquery have nothing to do with each
other..(based on inputs I could gather from people on other forums).
This piece of information would really help....but..
In my case they both to work separately use something in common to the
html symbol..As a result I have been able to get them both working
separately.. but not yet together.. Please find below the details of
what exactly is common among them and what separates the two based on my
observation
Firstly in order to get posting of message to work for me with AJAX, i
had to apart from adding several change elsewhere also make sure that
within my _common.html.erb partial view I add an :id => 'with_some_name'
within my html symbol. Something like this:-
- :html => { :multipart => :true , :id => 'new_post'}
within my form_for
tag.. Now why exactly this works this way.. I haven't yet figured out
and I am researching on the same..
So this basically was working fine until I had integrated uploading of
images feature into my code..
Initially I was facing a roadblock in passing my image as a params.. I
observed that it was not being passed when I looked into my log file..
The reason behind this, based on what I could figure out, as a solution
to get image upload working for me was... when I make use of
- :html => {
- :multipart => :true , :id => 'new_post'}
in my _common.html.erb partial
view, originally as you can see the 'multipart' and 'id' symbol are
taken as if as params for the html symbol.. and may be for the entire
html.erb partial that is being rendered..
I observed that when I removed the id symbol from its original location
as you can see above.. to something like this:-
- :html => { :multipart => :true }, :id => 'new_post'
images were now
being taken as a part of params along with each message.
html was being rendered and the page reloads..displaying the image and
the post.. but this doesn't render the .js.erb file any more.. .
why exactly this contrast in behavior is what I am unable to
understand.. and how exactly to get both working is something.. I am
trying to figure out..I guess I need to first understand exactly why
each thing works as shown..
Thank you very much.