Skip to content

Instantly share code, notes, and snippets.

@staycreativedesign
Created March 16, 2019 22:49
Show Gist options
  • Save staycreativedesign/83f9b571d2210de1e78a378eb7bd7423 to your computer and use it in GitHub Desktop.
Save staycreativedesign/83f9b571d2210de1e78a378eb7bd7423 to your computer and use it in GitHub Desktop.
Started GET "/presign?filename=bar.jpg" for 127.0.0.1 at 2019-03-16 17:47:32 -0500
Started GET "/presign?filename=foo.pdf" for 127.0.0.1 at 2019-03-16 17:47:32 -0500
Started PATCH "/admin/courses/foobar/lessons/part-1-introduction-to-fun-stuff" for 127.0.0.1 at 2019-03-16 17:48:20 -0500
Processing by Admin::LessonsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/5MUr1oLT/vaBhy2wnxiZONrx6c21PYshnIdrZAv8uahgoPglKbNujCTu9tXFB2eakk5Doy5ChyNGYRzHAQPxQ==", "lesson"=>{"title"=>"Part 1: Introduction to Fun stuff", "video_link"=>"https://player.vimeo.com/video/316481875?byline=0&portraitrtrait=0", "documents_attributes"=>{"graphic"=>"{\"id\":\"171e46b1408211f235a504146b72e321.pdf\",\"storage\":\"cache\",\"metadata\":{\"size\":361072,\"filename\":\"TheRealMessiah.pdf\",\"mime_type\":\"application/pdf\"}}"}, "description"=>"<p>This is going to be intrsi</>eresting</p>"}, "uploading_image"=>"", "commit"=>"Update Lesson", "course_id"=>"kaballah-of-you", "id"=>"part-1-introduction-to-fun-stuff"}
Membership Load (0.7ms) SELECT "memberships".* FROM "memberships" WHERE "memberships"."id" = $1 LIMIT $2 [["id", 51], ["LIMIT", 1]]
Course Load (0.5ms) SELECT "courses".* FROM "courses" WHERE "courses"."slug" = $1 LIMIT $2 [["slug", "kaballah-of-you"], ["LIMIT", 1]]
Lesson Load (0.6ms) SELECT "lessons".* FROM "lessons" WHERE "lessons"."slug" = $1 LIMIT $2 [["slug", "part-1-introduction-to-fun-stuff"], ["LIMIT", 1]]
(0.1ms) BEGIN
(0.2ms) ROLLBACK
Completed 500 Internal Server Error in 16ms (ActiveRecord: 7.9ms)
NoMethodError (undefined method `with_indifferent_access' for #<String:0x00007ffa0607b928>):
app/controllers/admin/lessons_controller.rb:41:in `update'
%section
.row
.col-sm-6
#graphic_upload
#ProgressBar
= f.fields_for :documents_attributes do |ff|
= ff.hidden_field :graphic, class: "upload-hidden"
%script{:src => "https://transloadit.edgly.net/releases/uppy/v0.27.3/dist/uppy.min.js"}
:javascript
$R('#lesson_description', { plugins: ['video', 'alignment', 'counter'] });
function fileUpload(fileInput) {
var uppy = Uppy.Core({ autoProceed: true })
.use(Uppy.FileInput, { target: '#graphic_upload',
pretty: true,
inputName: 'uploading_image',}) // adds a pretty file field
.use(Uppy.ProgressBar, { target: "#ProgressBar" , fixed: false, hideAfterFinish: true}) // displays a progress bar
.use(Uppy.Informer, { /* ... */ }) // displays validation errors
uppy.use(Uppy.AwsS3, {
getUploadParameters: function (file) {
return fetch('/presign?filename=' + file.name, {
credentials: 'same-origin', // send cookies
})
.then(function (response) { return response.json() })
}
})
uppy.on('upload-success', function (file, data, uploadURL) {
var uploadedFileData = JSON.stringify({
id: uploadURL.match(/\/cache\/([^\?]+)/)[1], // extract key without prefix
storage: 'cache',
metadata: {
size: file.size,
filename: file.name,
mime_type: file.type,
}
})
// set hidden field value to the uploaded file data so that it's submitted with the form as the attachment
var hiddenInput = document.getElementById('lesson_documents_attributes_graphic')
hiddenInput.value = uploadedFileData
})
return uppy
}
document.querySelectorAll('#graphic_upload').forEach(function (fileInput) {
fileUpload(fileInput)
})
class Lesson < ApplicationRecord
extend FriendlyId
friendly_id :title, use: [:finders, :slugged]
belongs_to :course
has_many :documents, as: :documentable, dependent: :destroy
accepts_nested_attributes_for :documents, update_only: true
def should_generate_new_friendly_id?
slug.blank? || title_changed?
end
end
def lesson_params
params.require(:lesson).permit(:course_id, :video_link, :title, :slug, :description, documents_attributes: [:graphic] )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment