Running with Ruby

Tag: SimpleForm

Rails + CoffeeScript + Jquery – Marking multiply objects for destruction at a time (frontend)

When we build complicated nested forms (for example with 2 or more nested resources in it), quite often we would like to add a multiply remove feature. For example to remove whole groups of resources. We can’t do this by simply removing HTML form parts, because objects related to this form parts won’t be deleted. Instead, Rails use a
_destroy input that should be set to true, when we want to remove an object (but the form part still needs to be sent).

<input 
  type="hidden" 
  value="false" 
  name="product[groups_attributes][0][_destroy]" 
  id="product_groups_attributes_0__destroy" 
  class="hidden"
>

It is quite easy to use when we remove one resource at a time, we can just do this:

$('#product').find( 'input:hidden' ).val(true)

Of course it is far from being perfect, but for one resource it should do the job. We don’t search using _destroy, we just assume, that only the hidden field that is there is the one that we want. Bad idea

What should we do, when we have multiply hidden fields and we only want to set value for those responsible for destruction? Well, we can use a nice Jquery feature: filter with a regexp.

destroy: (node) ->
  node.hide()
  node.find('input').filter ->
    return this.id.match(/__destroy/)
  .val('true')

We get a node (this is the tree part, that we want to remove), we iterate through all the inputs and we take only those, that have an __destroy postfix in their ID. Then we set all of them to true and we hide the whole node (so user will think, that it was removed). After we send such a form, Rails will know that it should remove all objects that were marked for destruction.

Paperclip, Bootstrap and SimpleForm working together on Rails

Few days ago, I’ve decided to get back to the “original” Paperclip. Until now, in one of my projects I’ve been using forked version with additional tweaks. However, supporting it for 3 years was enough. Getting back on track was fairly simple. It took me 1 day to fix the file structure, next day to rewrite validators and that would be all except one thing. Paperclip attaches error to 3 fields (example for thumb file):

  • thumb_file_name
  • thumb_file_size
  • thumb_content_type

I don’t like this idea, since those are basically the internals of Paperclip implementation and in my opinion, all the errors should be attached to the “base” attribute (which in this case is called thumb). Furthermore this is not only the architectural problem but it also affect my views. I use Bootstrap with a SimpleForm attached to it and adding Paperclip to it seams fairly simply:

= simple_form_for @user, :html => { :class => 'form-horizontal' } do |f|
  = f.input :avatar

And… this should be it. However, as I mentioned above, Paperclip attaches errors to fields different than “thumb” (not all of them but it doesn’t matter), so they aren’t displayed on the interface. I could use something like that:

= f.errors :thumb_content_type

and well, this indeed works, unfortunately without any Bootstrap stylings. Also it requires extra line for each attachment that I use. So it sucks! That’s why I’ve decided to do a little hack on it: let’s just copy all the error messages into original “base” attachment name. This should solve our problem (and it did):

# lib/paperclip_extensions.rb
module PaperclipExtensions

  extend ActiveSupport::Concern

  module ClassMethods
    # Changes the default Paperclip behaviour so all the errors from attachments
    # are assigned to an attachment name field instead of 4 different once
    # This allows us to use Bootstrap and SimpleForm without any other extra
    # code
    def has_attached_file(name, options = {})
      # Initialize Paperclip stuff
      super
      # Then create a hookup to rewrite all the errors after validation
      after_validation do
        self.errors[name] ||= []
        %w{file_name file_size content_type updated_at}.each do |field|
          field_errors = self.errors["#{name}_#{field}"]
          next if field_errors.blank?

          self.errors[name] += field_errors
          field_errors.clear
        end
        self.errors[name].flatten!
      end
    end
  end

end

ActiveRecord::Base.send(:include, PaperclipExtensions)

Just put this code into a file in lib/ and create an initializer where you will require it:

# config/initializers/paperclip_extensions.rb
require 'paperclip_extensions'

After that, you are ready to go :) Enjoy using Paperclip with Bootstrap and SimpleForm without any problems!

Copyright © 2018 Running with Ruby

Theme by Anders NorenUp ↑