Skip to content

Instantly share code, notes, and snippets.

@cmthakur
Last active October 16, 2019 10:42
Show Gist options
  • Save cmthakur/0bf8e0e2b06e21a028c4c913cfc30359 to your computer and use it in GitHub Desktop.
Save cmthakur/0bf8e0e2b06e21a028c4c913cfc30359 to your computer and use it in GitHub Desktop.
activestorage
# Model
class Company < ApplicationRecord
has_many_attached :attachments
end
# Controller
def create
@company = Company.new(company_params)
attachments = params[:company][:attachments]
respond_to do |format|
if @company.save
end
if attachments
@company.attachments.attach(attachments)
end
format.html { redirect_to @company, notice: 'Company was successfully created.' }
format.json { render :show, status: :created, location: @company }
else
format.html { render :new }
format.json { render json: @company.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /companys/1
# PATCH/PUT /companys/1.json
def update
attachments = params[:company][:attachments]
respond_to do |format|
if @company.update(company_params)
end
if attachments
@company.attachments.attach(attachments)
end
format.html { redirect_to @company, notice: 'Company was successfully updated.' }
format.json { render :show, status: :ok, location: @company }
else
format.html { render :edit }
format.json { render json: @company.errors, status: :unprocessable_entity }
end
end
end
# View
<div class="field">
<%= form.label :attachments %>
<%= form.file_field :attachments, multiple: true %>
</div>
# Model
class Profile < ApplicationRecord
has_one_attached :avatar
accepts_nested_attributes_for :avatar_attachment, allow_destroy: true
end
# Controller
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
# [...]
# PATCH/PUT /profiles/1
# PATCH/PUT /profiles/1.json
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = Profile.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def profile_params
params.require(:profile).permit(
:last_name, :first_name,
:avatar,
avatar_attachment_attributes: [:id, :_destroy]
)
end
end
# View
<p id="notice"><%= notice %></p>
<p>
<strong>Avatar:</strong><br />
<% if @profile.avatar.attached? %>
<%= form_for @profile do |f| %>
<%= f.fields_for :avatar_attachment_attributes do |avatar_form| %>
<%= avatar_form.hidden_field :id, value: @profile.avatar_attachment.id %>
<%= avatar_form.hidden_field :_destroy, value: true %>
<% end %>
<%= f.submit "Delete avatar" %>
<% end %>
<% end %>
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment