Guest User

Untitled

a guest
Oct 24th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. # Attachinary Setup
  2.  
  3. First add the following gems to your `Gemfile`:
  4.  
  5. ```ruby
  6. # Gemfile
  7. gem "attachinary"
  8. gem "jquery-fileupload-rails"
  9. gem "coffee-rails"
  10. ```
  11.  
  12. Then open the terminal and launch:
  13.  
  14. ```bash
  15. bundle install
  16. rails attachinary:install:migrations
  17. rails db:migrate
  18. ```
  19.  
  20. Open your `config/application.rb` and add this line after all the `require`:
  21.  
  22. ```ruby
  23. require "attachinary/orm/active_record"
  24. ```
  25.  
  26. Open the `config/routes.rb` file and add this as first line in the `draw` block:
  27.  
  28. ```ruby
  29. mount Attachinary::Engine => "/attachinary"
  30. ```
  31.  
  32. Open `app/views/layout/application.html.erb` and append this line after the main `javascript_include_tag`:
  33.  
  34. ```erb
  35. <%= cloudinary_js_config %>
  36. ```
  37.  
  38. Open `app/assets/javascripts/application.js` and append these lines before the `require_tree`:
  39.  
  40. ```js
  41. //= require jquery-fileupload/basic
  42. //= require cloudinary/jquery.cloudinary
  43. //= require attachinary
  44. ```
  45.  
  46. Create a file `app/assets/javascripts/init_attachinary.js` and copy-paste those lines:
  47.  
  48. ```
  49. $(document).ready(function() {
  50. $('.attachinary-input').attachinary();
  51. });
  52. ```
  53.  
  54. # Usage
  55.  
  56. ## One picture per model
  57.  
  58. You need to update the model:
  59.  
  60. ```ruby
  61. class Product < ApplicationRecord
  62. has_attachment :photo
  63.  
  64. # [...]
  65. end
  66. ```
  67.  
  68. And the form (`simple_form` gem used):
  69.  
  70. ```erb
  71. <%= f.input :photo, as: :attachinary %>
  72. ```
  73.  
  74. And the controller for strong params:
  75.  
  76. ```ruby
  77. def product_params
  78. params.require(:product).permit(:name, :description, :photo)
  79. end
  80. ```
  81.  
  82. To display the product photo in the view, add:
  83.  
  84. ```erb
  85. <% if @product.photo? %>
  86. <%= cl_image_tag @product.photo.path %>
  87. <% end %>
  88. ```
  89.  
  90. ## Multiple pictures per model
  91.  
  92. You need to update the model:
  93.  
  94. ```ruby
  95. class Product < ApplicationRecord
  96. has_attachments :photos, maximum: 2 # Be carefule with `s`
  97.  
  98. # [...]
  99. end
  100. ```
  101.  
  102. And the form (`simple_form` gem used):
  103.  
  104. ```erb
  105. <%= f.input :photos, as: :attachinary %>
  106. ```
  107.  
  108. And the controller for strong params:
  109.  
  110. ```ruby
  111. def product_params
  112. params.require(:product).permit(:name, :description, photos: [])
  113. end
  114. ```
  115.  
  116. To display the product photos in the view, add:
  117.  
  118. ```erb
  119. <% @product.photos.each do |photo| %>
  120. <%= cl_image_tag photo.path %>
  121. <% end %>
  122. ```
  123.  
  124. ## Bonus - Devise
  125.  
  126. If you want to add an `avatar` to the `User` model, you need to sanitize regarding the strong params:
  127.  
  128. ```ruby
  129. # app/controllers/application_controller
  130. class ApplicationController < ActionController::Base
  131. before_action :configure_permitted_parameters, if: :devise_controller?
  132.  
  133. def configure_permitted_parameters
  134. devise_parameter_sanitizer.permit(:sign_up, keys: [:avatar])
  135. devise_parameter_sanitizer.permit(:account_update, keys: [:avatar])
  136. end
  137. end
  138. ```
Add Comment
Please, Sign In to add comment