Hello!

New to rails (and very little knowledge of js), so sorry for such a question. There is a model House you need to create a form with properties and photos. At first, the carrierwave gem was used rather rudely, for example, if you need to add several photos, you need to use ctrl (there is no possibility to add one photo to the form or delete the extra one).

I read about jQuery-File-Upload. Judging by this example https://github.com/blueimp/jQuery-File-Upload/wiki/Rails-setup-for-V6, you will have to create a Picture model and apparently link the models using has_many belongs_to. But then what form do you need or do you need to make two forms for each model separately and pass for Pictures id_house?

    1 answer 1

    At first I used carrierwave gem rather rudely.

    Roughly not at all because of the carrierwave , but because you cannot immediately attach several files to the input . You just need a lot of input and on the server side, process them with a carrierwave , you can even deferred. And yes, in the example you wrote that you need a Picture model, and in it the carrierwave fields are carrierwave . But it was obvious and without using carrierwave . those. if you have a house, then it has a lot of pictures (in Russian, it has a lot of pictures). Here you have a description of the connection, we get:

     class House has_many :pictures accepts_nested_attributes_for :pictures end class Picture belongs_to :house #... всё что нужно для carrierwave end 
    1. The first option, the easiest thing that comes to mind is to use nested records:

       class House has_many :pictures accepts_nested_attributes_for :pictures end 

      Nested entries are set using the class method ::accepts_nested_attributes_for , which allows you to use nested entries when saving, in your case it is necessary that the attached photos are attached to the house. Please note that using this property you can also delete nested records, and not just add them.

      Although in the description there is even an example of code for loading, however, since it is not quite what it is required, you can do something like this: you make one form, for the house, you just include the image attribute fields in it, and they are such a non-weak array that all go when updating to the server:

       <%= form_for @house, url: houses_path } do |f| %> # ... <% f.fields_for @house.pictures do |pic| %> <%= pic.file_field :avatar %> #... тут уже код идёт из примера с jQuery-File-Upload <%- end %> <%- end %> 

      Also, do not forget to properly handle the nested entries in the controller on strong parameters.

    2. You can do as you suggest, that is, a form for the home and many forms for photos, each in form. Only here the restriction is that the form of photos can appear only if the house entry has already been created, and yes, in the form of a photo, there should be a field specifying the house to which the photo belongs. Then you can do without nested records.

    • Thanks, using the first method I stumbled upon a problem with update, that is, a new record is added to the place where the record is updated in the photo table (that is, there are two photos in one house instead of one replaced ... well, etc. 3, 4 ... ) read stackoverflow.com/questions/9944065/… here about this solution config.action_controller.action_on_unpermitted_parameters =: raise, but there is an error in the console that is missing id .. - nikfor
    • @nikfor what id is missing? there, in the first version, the problem is that all the photos will be updated (by design), even those that were saved before, and this is not cool, you should play the console, see which parameters simply do not change the properties of the picture, for example, give him the real id of the picture, not her own - Mal Skrylev
    • @nikfor there in the decision on the link write. that an id is needed to update and not create new objects, I fully agree, moreover, an unaffected object does not need to be sent to the server, but only its id - Mal Skrylev 15
    • Thank you very much for your responsiveness! Yes you are right, I myself blunted a little. that is, I wrote something like this to house.photos.update (image: pictures_attributes (: image)) and did not transmit the photo id. sql without knowing id created a new record ... now I first delete all the photos and then re-create, it is assumed that all photos are preloaded in the form fields from the server and new from the user's computer. - nikfor
    • one
      @nikfor is not for nothing, but perhaps every time it is difficult to re-create entries for photos. - Mal Skrylev