使用CarrierWave_Direct上传的问题

时间:2016-03-07 00:49:05

标签: ruby-on-rails ruby file-upload amazon-s3 carrierwave

我对ruby和rails很新,我在使用carrierwave_direct上传时遇到了一些问题。我使用carrierwave_direct上传了Ryan Bates的Railscast 383。但是,他提供的解决方案似乎并不适合我。我有一个使用Devise创建的User模型,我有一个上传器来处理我的上传。我正在尝试使用上传器上传视频文件,我计划稍后使用带有AWS API的Elastic Transoder进行转码。这些文件正好上传到s3,但似乎它们没有与我的数据库中的记录相关联。文件在我提交后立即上传,但我需要一种方法来创建它们以及标题,描述,以后可能还有其他参数。我的if-else语句也总是重定向到我的else语句。我想我的问题在于我的控制器;我的路线和观点似乎工作得很好,我只是坚持这个问题。

进一步澄清:我需要用户能够上传视频,然后让用户输入标题,描述等,然后重定向到另一个页面。然后,我需要能够在以后显示该文件。

这是我的UploadsController:

class UploadsController < ApplicationController
before_action :authenticate_user!, only: [:create, :destroy]
before_action :current_user, only: :destroy

  def index
    @uploader = Upload.new.video
    @uploader.success_action_redirect = new_uploads_url
  end

  def show
    @upload = Upload.find(params[:id])
  end

  def new
    @upload = Upload.new(key: params[:key])
  end

  def create
    @upload = Upload.new(upload_params)
      if @upload.save
      redirect_to home_index_path
      return
    else
      redirect_to uploads_index_path
    end
  end

  def edit
  end

  def destroy
    @upload = Upload.find(params[:id])
    @upload.destroy
    flash[:notice] = "Upload deleted"
    redirect_to request.referrer || root_url
  end


  def upload_params
    params.require(:upload).permit(:video, :title, :description)
  end
end

这是我的上传模型:

class Upload < ActiveRecord::Base
  belongs_to :user
  default_scope -> { order(created_at: :desc) }
  validates :user_id, presence: true
  validates :description, presence: true
  validates :title, presence: true, length: {maximum: 100}
  validates :description, presence: true
  validates :video, presence: true
  mount_uploader :video, VideoUploader

  after_save :enqueue_video

  def enqueue_video
    VideoWorker.perform_async(id, key) if key.present?
  end

 class VideoWorker
    include Sidekiq::Worker

    def perform(id, key)
     upload = Upload.find(id)
     upload.key = key
     video.remote_video_url = upload.video.direct_fog_url(with_path: true)
     upload.save!
    end
 end
end

我的路线:

Rails.application.routes.draw do

  get '/home/index'

  root 'home#index'

  devise_for :users

  get 'users/:id' => 'users#show'

  resources :uploads

  get '/index' => 'uploads#index'

end

编辑以下是我的UsersController和用户显示页面:

控制器:     class UsersController&lt; ApplicationController中

  def show 
    @user = User.find(params[:id])
  end

end

显示:

<h1><%= @user.username %></h1>

<h3>Title:</h3>
<%= @upload.title %>

1 个答案:

答案 0 :(得分:0)

问题是你在做什么:

def create
    @upload = Upload.new(upload_params)
    # [..]
end

def upload_params
    params.require(:upload).permit(:video, :title, :description)
end

您的UploadUser相关联:

class Upload < ActiveRecord::Base
    belongs_to :user
    validates :user_id, presence: true
    # [..]
end

那么Rails如何知道哪个Upload属于哪个User?你从来没有告诉过Upload任何事情&#39;不是User

如果您总是想使用当前登录的用户,您可能会执行以下操作:

def create
    @upload = Upload.new(upload_params)
    @upload.user = current_user
    # [...]
end

当然,这假设current_user是获取当前登录用户的正确方法(通常是这样)。

如果您希望能够将其与任何其他用户相关联,则需要在其中添加user_id字段并将其添加到upload_params