On May 22, 2011, at 6:58 AM, Mohnish J. wrote:

Hi Walter,

Thanks for your reply

Sorry for replying in late.. I had seen your post in the morning
itself.. was just doing research from my end..

To give in something that could help my case...

Actually, if you can kindly spare some time... to best explain my case.. I am pretty much doing what has been done in the Railscast by Ryan Bates
Jquery( http://railscasts.com/episodes/136-jquery )..I am actually
overriding the the submit method.... The only difference lies in the
fact that I am trying to display a new image which comes along with each
new message posted..

After some researching I found these quotes very interesting:-

"
5.2 Dealing with Ajax
Unlike other forms making an asynchronous file upload form is not as
simple as replacing form_for with remote_form_for. With an Ajax form the serialization is done by JavaScript running inside the browser and since
JavaScript cannot read files from your hard drive the file cannot be
uploaded. The most common workaround is to use an invisible iframe that
serves as the target for the form submission.
"

This is taken from the site:-
http://guides.rubyonrails.org/form_helpers.html#dealing-with-ajax



This actually compliments what Fred had mentioned previously..
..would like to requote that:-

[

"




Posted by Frederick Cheung (Guest)
on 2011-05-21 12:39
(Received via mailing list)

On May 21, 10:59am, "Mohnish J." <[email protected]> wrote:
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-......
.

Traditionally, a straight up ajax form can't upload files: when using
ajax a piece of javascript serializes the form and sends the request
to the server and javascript running inside the browser doesn't have
access to the filesystem.

The illusion of an ajax file upload can be provided by having the file
upload happen inside a hidden iframe, there are plenty of articles
describing how this works (and probably several jquery plugins
implementing it). Other options involve using flash for the upload

The file-api specification (http://www.w3.org/TR/file-upload/) does
add the ability for javascript to access the filesystem but not all
browsers currently support this

Fred

"

]


I tried to better understand what was happening for me .. based on the
part of AJAX, that I could implement.. I found that as in my first
post.. the latest_post represents "all" the entries of all messages
posted so far along with images as shown in the partial.. the only thing
that is happening is that the "latest" message is being "prepended" or
one can say prefixed in a way to the already existing list.. so that
list is not being fetched again from the DB and its just moved a
step/level down giving place for the latest post to come.. All this is
cool..:) but now... to get the same working with each image.. is my real
challenge going forward....


Also since your also unfamiliar with the jquery syntax..

I found this as a good substitute to start somewhere:-

http://www.williambharding.com/blog/rails/rails-ajax-image-uploading-made-simple-with-jquery/

He has have made use of the attachment-fu plugin..

Now my challenge is to somehow use them together to get it working for
me..
I am working on it...Javascript is not something that I am quite good at though..:).. will do what best I can from my end.. would be grateful if
you could help me with the your inputs , if you get time checking out
the above urls..

Thanks again Walter..:)


Walter Davis wrote in post #1000100:
On May 21, 2011, at 12:38 PM, Mohnish J. wrote:

put the button..
<div id = "latest_post"> </div>
<%end%>


I would truly appreciate you extra effort in trying to help me..

Kindly bear with my being a novice.

Many Thanks for your inputs..

I'm looking at this code, and it appears to be a normal post to the
server. Where and what do you plan to update to the page using jQuery? Are you saying you want to post to the server, but through JavaScript,
and update the current page with a refresh of the list of messages?
Because there's a well-worn way to do that. You stop the form.submit
method, and use Ajax to send the form instead, then your server
replies with the current list of messages, and you update the
appropriate list DIV with those.

I don't know the jQuery idiom for this, but in Prototype, it's very
simple:

$('your_form_id').observe('submit',function(evt){
 evt.stop(); //keep the default submit from happening
 this.request({ //send the form through an Ajax request
   onSuccess: function(transport){
     $('your_list_container').update(transport.responseText);
   }
 });
});

As to the conditional, it doesn't apply in this pattern. But let's say
you had a form submit button, and you wanted it to stop working after
the form had been submitted (sort of what :disable_with does in the
regular form builder code). You want the button to be pushed once
only, so you might do something like:

$('your_button').observe('click',function(evt){
 if ( this.clicked ){
   evt.stop();
 }else{
   this['clicked'] = true;
 }
});

So the first time through, the button click happens and bubbles (and
the button is marked so no future clicks will work) and the second +
times it is clicked, it does nothing, because Event.stop happens
instead. (Event.stop is a belt-and-suspenders form of
Event.preventDefault, which is built in to most browsers, and I
suspect added by jQuery to those that don't have it. It just makes the
current event die and never propagate beyond the current element.

Walter


So is your question then how to go about uploading the file through "faux Ajax" methods, like a hidden iframe? Have a quick search on Github -- I would be startled if you didn't find a few ways to do this using a gem or plugin. The code itself is very simple, all you have to do is add the target attribute to your form, and make the value of that attribute the name and/or id of an iframe you have styled to be invisible. Everything else is the same. Then to get the response back from that iframe, you need to do a little more JavaScripting.

Walter

--
You received this message because you are subscribed to the Google Groups "Ruby on 
Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to