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();
});
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 %>
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 %>
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
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