I want to refresh div content after form submit
So, I am using a :remote => true
form with the paperclip gem to upload a photo. When I submit the form I just want to refresh the content of a div (which will now contain the uploaded photo).
Rails, however, insist开发者_开发问答s on redirecting me to the path of the action used to process the photo submission. Additionally, there is the issue of actually refreshing the div.
I would like to do it with javascript, but there is no afterSubmit handler or anything. I can't figure out how to respond to the form submit with javascript either. I tried to do
def add_photo
SomeModel.method_that_processes_and_saves(params)
respond_to do |format|
format.js
end
end
and placing the script in add_photo.js.erb
. However, the script is never sent (the browser is asking for html?) How can I get around this?? Its been frustrating me for a whole day now.
def add_photo
SomeModel.method_that_processes_and_saves(params)
respond_to do |format|
format.js{ render :update do |page|
page << "$('#mydiv').html('bah-blah') "
end
end
Or create add_photo.js.erb
and write there any javascript you want.
It will work if you're submitting your form via AJAX (js request), that mean that you should add :remote => true
option
There's no concept of "afterSubmit" because the page has been replaced by the form submission. The simplest way to tackle this is to handle the photo upload on the subsequent page. Ruby should give you the path. Another approach is to use one of the many upload libraries to asynchronously do the upload and give you a "on complete" callback. http://www.uploadify.com/ is a popular choice.
精彩评论