Skip to content

Instantly share code, notes, and snippets.

@firmanm
Forked from ssaunier/attachinary_setup.md
Created October 25, 2017 06:41
Show Gist options
  • Select an option

  • Save firmanm/2a1e86353536a711b412679e3ec1aa0d to your computer and use it in GitHub Desktop.

Select an option

Save firmanm/2a1e86353536a711b412679e3ec1aa0d to your computer and use it in GitHub Desktop.

Revisions

  1. @ssaunier ssaunier revised this gist Mar 17, 2016. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions attachinary_setup.md
    Original file line number Diff line number Diff line change
    @@ -119,4 +119,20 @@ 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:

    ```ruby
    # 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
    ```
  2. @ssaunier ssaunier revised this gist Mar 17, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion attachinary_setup.md
    Original file line number Diff line number Diff line change
    @@ -87,7 +87,7 @@ To display the product photo in the view, add:
    <% end %>
    ```

    ## Multiple attachments per model
    ## Multiple pictures per model

    You need to update the model:

  3. @ssaunier ssaunier revised this gist Mar 17, 2016. 1 changed file with 93 additions and 2 deletions.
    95 changes: 93 additions & 2 deletions attachinary_setup.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,6 @@ First add the following gems to your `Gemfile`:

    ```ruby
    # Gemfile
    gem "cloudinary"
    gem "attachinary"
    gem "jquery-fileupload-rails"
    gem "coffee-rails"
    @@ -24,8 +23,100 @@ 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.
    Open the `config/routes.rb` file and add this as first line in the `draw` block:

    ```ruby
    mount Attachinary::Engine => "/attachinary"
    ```

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

    ```erb
    <%= cloudinary_js_config %>
    ```

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

    ```js
    //= 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:

    ```ruby
    class Product < ApplicationRecord
    has_attachment :photo

    # [...]
    end
    ```

    And the form (`simple_form` gem used):

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

    And the controller for strong params:

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

    To display the product photo in the view, add:

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

    ## Multiple attachments per model

    You need to update the model:

    ```ruby
    class Product < ApplicationRecord
    has_attachments :photos, maximum: 2 # Be carefule with `s`

    # [...]
    end
    ```

    And the form (`simple_form` gem used):

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

    And the controller for strong params:

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

    To display the product photos in the view, add:

    ```erb
    <% @product.photos.each do |photo| %>
    <%= cl_image_tag photo.path %>
    <% end %>
    ```
  4. @ssaunier ssaunier revised this gist Mar 17, 2016. No changes.
  5. @ssaunier ssaunier renamed this gist Mar 17, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md → attachinary_setup.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Attachinary Setup

    First add the following gems to your `Gemfile`:

    ```ruby
  6. @ssaunier ssaunier created this gist Mar 17, 2016.
    29 changes: 29 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    First add the following gems to your `Gemfile`:

    ```ruby
    # Gemfile
    gem "cloudinary"
    gem "attachinary"
    gem "jquery-fileupload-rails"
    gem "coffee-rails"
    ```

    Then open the terminal and launch:

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

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

    ```ruby
    require "attachinary/orm/active_record"
    ```

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

    ```ruby
    mount Attachinary::Engine => "/attachinary"
    ```