F
F
FanKiLL2014-09-09 10:52:04
JavaScript
FanKiLL, 2014-09-09 10:52:04

How to create an entry in rails if the entry dependent on it has not yet been saved and there is no id?

I ran into a funny problem.
For example, there are posts, a post can have many pictures via has_many.
I load pictures ajax and if the post has already been created, then there is no problem, send the post id, create a picture and attach it to the post. (edit)

The problem arises when I want to create a new post, where there is no id itself, since the model is not saved. And it turns out I can’t attach the picture anywhere because there is no id.

I load pictures ajax so that they can be immediately inserted into the post being created.

Are there any considerations in which direction to dig? Or did I take the wrong approach at all?
Doing nested_forms and saving pictures along with the post is not an option - when writing a post, you need to upload a new picture in one click and insert it into the post.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2014-09-09
@FanKiLL

You can create these pictures without specifying an owner, return their IDs as the result of an ajax request, and store those IDs in the form. When saving a post, look at what IDs of the pictures you received along with the form, and after saving the post, put down the ID of the owner.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question