Last active
October 2, 2021 09:44
-
-
Save poc7667/be13039f6cbaa65205a4 to your computer and use it in GitHub Desktop.
active admin / paper clip / images /
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveAdmin::ViewHelpers | |
include ApplicationHelper | |
def show_image(p) | |
p.object.image.nil? ? p.template.content_tag(:span, "No Upload Image Yet") : p.template.image_tag(p.object.image.url(:small)) | |
end | |
end | |
module ActiveAdmin | |
module Views | |
class Footer < Component | |
def build | |
super :id => "footer" | |
super :style => "text-align: right;" | |
div do | |
small "Cool footer #{Date.today.year}" | |
end | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveAdmin.register CoffeeCourse do | |
form :html => { :multipart => true } do |f| | |
f.inputs do | |
[:name, :teachers, :price, :price].each do |col| | |
f.input col | |
end | |
end | |
f.has_many :photos, heading: false, allow_destroy: true do |p| | |
p.input :image, :as => :file, :label => "Image",:hint => show_image(p) | |
p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image' | |
end | |
f.actions | |
end | |
permit_params [:name, :teachers, :price, :price, :photos_attributes=> [:id, :image, :_destroy]] | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CoffeeCourse < ActiveRecord::Base | |
has_many :photos, :dependent => :destroy | |
accepts_nested_attributes_for :photos, allow_destroy: true | |
def upload_images(params, image) | |
photos.create(title: params['name'], image: image) | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# controller | |
def create | |
@coffee_course = CoffeeCourse.new(coffee_course_params) | |
respond_to do |format| | |
if @coffee_course.save | |
@coffee_course.upload_images(product_params, params['photo']) if params['photo'] | |
format.html { redirect_to @coffee_course, notice: 'Coffee course was successfully created.' } | |
format.json { render :show, status: :created, location: @coffee_course } | |
else | |
format.html { render :new } | |
format.json { render json: @coffee_course.errors, status: :unprocessable_entity } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment