开发者

Using ajax for rails image preview in form

I've been working on this issue for about two days now. I've posted more task specific questions, got great answers, only to figure out the approach I was taking wo开发者_高级运维uldn't work.

Basically, I want a user to be able to upload images in a form and see a preview of the image they upload before submitting the form. The images and the parent form have a has_many relationship. The images are nested in the parent form using fields_for.

I tried using a client side approach, but ran into cross browser and security issues. My current approach I'm trying is to save the images, reload the div portion of the page, and then assign the parent_id to the image after the partent form is submitted (since I will not have a id for the partent form until it is created).

Is it possible to use ajax to submit the fields_for portion of a form and not the entire parent form? Has anyone attempted to do something similar?

Using ajax for rails image preview in form


Of course you can submit only the fields of that image by using jQuery to select only the fields that you want to send, serialize them and send them as data of the ajax request.

Be careful, you need to send the authtoken, that's why I have type=hidden in the selector

var data = form.find('[name*=image],[type=hidden]').not('[name*=items]').serialize(); $.ajax({url: this.form.attr('action'), context: this, type: 'POST', data: data

The next step with the image I once implemented as storing the in their own database table, returning the client the id to that who then adds it to the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜