Transfer of parameters to build while retained in Ruby on Rails


  • QA Engineer

    There are two models:

    class Arendakvartir < ActiveRecord::Base
    

    has_many :attachments, dependent: :destroy
    accepts_nested_attributes_for :attachments
    validates_associated :attachments

    end

    class Attachment < ActiveRecord::Base

    belongs_to :arendakvartir

    has_attached_file :image,
    :path => ":rails_root/public/images/:id/:filename",
    :url => "/images/:id/:filename",
    styles: { thumb: 'x100', croppable: '600x600>', big: '1000x1000>' }

    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"], :message => "Разрешены изображение формата: jpg, png, gif"
    validates_attachment :image, presence: true, :message => "Извини друг без картинок нынче никак"
    validates_attachment_size :image, :less_than => 8.megabytes, :message => "Какое то фото явно больше 8 МБ."

    end

    There's this in the counteraller:

      def new
    @arenda = Arendakvartir.new()
    15.times { @arenda.attachments.build() }
    end

    def create
    @arenda = Arendakvartir.new(arenda_params)

    if @arenda.save()
      @arenda.attachments.create
      respond_to do |format|
          flash[:positive] = "Объект сохранен успешно"
          format.html { redirect_to new_arendakvartir_path }
      end
    else
      respond_to do |format|
          flash[:negative] =  "Объект не был сохранен успешно"
          format.html { redirect_to :back  }
     end
    

    end
    end

    It creates 15 lines to add a file in a vortex. Here's the wow code:

     <%= t.fields_for :attachments do |ph| %>

      &lt;%= ph.file_field :image %&gt;
    

    <% end%>

    The question is, as much as I one line through multiple (multiple in shape) choices of many files while keeping them as one line @arenda.save()



  • Well, that's not true.

    Field of choice of files multiple It'll look like a bunch of files in the parameters.

    What you have is a set of parameters for you. Attachmentand that it adds an arbitrary number of photographs will require the addition/removal of forms through JS.

    You have two options.


    Leave me alone. Attachment

    Paperclip can work with fields where several files are located immediately, on one condition: the corresponding field in the model (to which the photos are attached) shall be mass. In this case, no nested attributes will be needed, and a separate model will also be needed.

    It's a good idea if your OBD supports the masses and no additional information is planned for the photo.


    ...or clean up the parameters.

    Band in the input filter for parameters (where strong parameters) convert the normal file content into a set of parameters Attachment:

    ….map! { |f| { image: f } }
    

    The form will still have to be changed to have a field of multiple choice of files. This field will have its name. attachments_attributesand usually it's written in fields_forto be able to complete several such forms and to obtain a range of models. But the field of choice of files is gonna send a mass, and if there's a change like that. fields_for No need.

    There's a small minus, more aesthetic: giving data from API in the same format will be uncomfortable: we have to change back. Although, in your case, it's hardly significant.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2