如何进行Ajax调用以在同一页面中创建帖子

时间:2019-06-25 05:30:04

标签: ruby-on-rails ruby-on-rails-5

我已经在控制器中创建了create.js和new.js文件。我在创建函数时遇到错误

这是我在后期控制器中的创建操作

        let currentAsset = AVAsset( url: outputFileURL)
        let composition = AVMutableComposition.init()

        let videoComposition = AVMutableVideoComposition()
        videoComposition.frameDuration = CMTimeMake(1, 30)
        videoComposition.renderScale  = 1.0

        let compositionCommentaryTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)

        let compositionVideoTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
        let videoDuration: CMTime = currentAsset.duration

        let clipVideoTrack:AVAssetTrack =  currentAsset.tracks(withMediaType: AVMediaType.video)[0]
        let audioTrack: AVAssetTrack = currentAsset.tracks(withMediaType: AVMediaType.audio)[0]

        try? compositionCommentaryTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, currentAsset.duration), of: audioTrack, at: kCMTimeZero)

        try? compositionVideoTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, currentAsset.duration ), of: clipVideoTrack, at: kCMTimeZero)
        var naturalSize = clipVideoTrack.naturalSize

        naturalSize = CGSize.init(width: naturalSize.width, height: naturalSize.height)
        videoComposition.renderSize = naturalSize
        let x = naturalSize.width
        let y = naturalSize.height

        let scale = CGFloat(1.0)
        let frontLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: compositionVideoTrack!)
        var rotationTransform = CGAffineTransform(translationX: x/2, y: y/2)
        rotationTransform = rotationTransform.rotated(by: angle)
        rotationTransform = rotationTransform.translatedBy(x: -x/2, y: -y/2)

        frontLayerInstruction.setTransform(rotationTransform, at: kCMTimeZero)

// saving video in gallery

        let MainInstruction = AVMutableVideoCompositionInstruction()
        MainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration)
        MainInstruction.layerInstructions = [frontLayerInstruction]
        videoComposition.instructions = [MainInstruction]
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let videoPath = documentsPath+"/cropEditVideo.mov"

        let fileManager = FileManager.default

        if fileManager.fileExists(atPath: videoPath) {
            try! fileManager.removeItem(atPath: videoPath)
        }

        var exportSession = AVAssetExportSession.init(asset: composition, presetName: AVAssetExportPresetHighestQuality)
        exportSession?.outputFileType = AVFileType.mov
        exportSession?.videoComposition = videoComposition
        exportSession?.outputURL = URL.init(fileURLWithPath: videoPath)
        .
        .
        .
        .

create.js.erb:

def create
  @post = @topic.posts.new(post_params.merge(user_id: current_user.id))
  respond_to do |format |
    if @post.save
      format.js { render action: "create" }
    else
      format.js { render action: "new" }
    end
  end
end

我的错误是<%if @post.errors.any? %> alert("<%= j @post.errors.full_messages.join('; ') %>"); <% else %> $('#new-post form').remove(); $("#new-skill-link").show(); $(".post-list ul").append('<%= j render(@post) %>'); alert("Post Successfully Created"); <% end %>

0 个答案:

没有答案