Skip to content

Instantly share code, notes, and snippets.

@ssaunier
Last active March 27, 2020 18:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save ssaunier/a32538935e6384af4c29 to your computer and use it in GitHub Desktop.
Save ssaunier/a32538935e6384af4c29 to your computer and use it in GitHub Desktop.
Attachinary Setup in Rails 5

Attachinary Setup

First add the following gems to your Gemfile:

# Gemfile
gem "attachinary"
gem "jquery-fileupload-rails"
gem "coffee-rails"

Then open the terminal and launch:

bundle install
rails attachinary:install:migrations
rails db:migrate

Open your config/application.rb and add this line after all the require:

require "attachinary/orm/active_record"

Open the config/routes.rb file and add this as first line in the draw block:

mount Attachinary::Engine => "/attachinary"

Open app/views/layout/application.html.erb and append this line after the main javascript_include_tag:

<%= cloudinary_js_config %>

Open app/assets/javascripts/application.js and append these lines before the require_tree:

//= require jquery-fileupload/basic
//= require cloudinary/jquery.cloudinary
//= require attachinary

Create a file app/assets/javascripts/init_attachinary.js and copy-paste those lines:

$(document).ready(function() {
  $('.attachinary-input').attachinary();
});

Usage

One picture per model

You need to update the model:

class Product < ApplicationRecord
  has_attachment :photo
  
  # [...]
end

And the form (simple_form gem used):

<%= f.input :photo, as: :attachinary %>

And the controller for strong params:

def product_params
  params.require(:product).permit(:name, :description, :photo)
end

To display the product photo in the view, add:

<% if @product.photo? %>
  <%= cl_image_tag @product.photo.path %>
<% end %>

Multiple pictures per model

You need to update the model:

class Product < ApplicationRecord
  has_attachments :photos, maximum: 2  # Be carefule with `s`
  
  # [...]
end

And the form (simple_form gem used):

<%= f.input :photos, as: :attachinary %>

And the controller for strong params:

def product_params
  params.require(:product).permit(:name, :description, photos: [])
end

To display the product photos in the view, add:

<% @product.photos.each do |photo| %>
  <%= cl_image_tag photo.path %>
<% end %>

Bonus - Devise

If you want to add an avatar to the User model, you need to sanitize regarding the strong params:

# app/controllers/application_controller
class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up,        keys: [:avatar])
    devise_parameter_sanitizer.permit(:account_update, keys: [:avatar])
  end
end
@jakobdamjensen
Copy link

Have you had any issues with the create action in your controller? I've had to jump through hoops to make it work whereas editing a model just works with changing attachments.

I've had to save the model before adding the attachments to it afterwards in an update call on the model for it to work.

@francelwebdev
Copy link

Bonjour Mr,
J'essaie d'utiliser les gem attachinary, cloudinary et jquery-fileupload dans un application rails pour l'upload de photo et j'ai cet erreur.

/home/francel/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/bundler/gems/attachinary-98a895be22ed/lib/attachinary/orm/active_record/file.rb:2:in <module:Attachinary>': uninitialized constant ActiveRecord (NameError) from /home/francel/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/bundler/gems/attachinary-98a895be22ed/lib/attachinary/orm/active_record/file.rb:1:in <top (required)>'
from /home/francel/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/bundler/gems/attachinary-98a895be22ed/lib/attachinary/orm/active_record.rb:4:in `require_relative'

Merci de m'aider

@jasonaloi
Copy link

How do we handle Non Image attachments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment