Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 获取rails控制器中Carrierwave上载的文件名_Ruby On Rails_File Upload_Carrierwave - Fatal编程技术网

Ruby on rails 获取rails控制器中Carrierwave上载的文件名

Ruby on rails 获取rails控制器中Carrierwave上载的文件名,ruby-on-rails,file-upload,carrierwave,Ruby On Rails,File Upload,Carrierwave,我需要在我的控制器中获取文件名和上载的文件,以便在用户未指定标题(或名称)的情况下为上载的文件设置默认标题(或名称)。我使用Carrierwave上传文件 我的控制器创建操作如下所示: def create @photo = Photo.new(params[:photo]) @photo.user_id = current_user.id respond_to do |format| if @photo.save format.htm

我需要在我的控制器中获取文件名和上载的文件,以便在用户未指定标题(或名称)的情况下为上载的文件设置默认标题(或名称)。我使用Carrierwave上传文件

我的控制器创建操作如下所示:

  def create
    @photo = Photo.new(params[:photo])
    @photo.user_id = current_user.id


    respond_to do |format|
      if @photo.save
        format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
        format.json { render action: 'show', status: :created, location: @photo }
      else
        format.html { render action: 'new' }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end
  def create
    @photo = Photo.new(params[:photo])
    @photo.user_id = current_user.id

    @photo.name =  @photo.image.file.filename if @photo.name == ""

    respond_to do |format|
      if @photo.save
        format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
        format.json { render action: 'show', status: :created, location: @photo }
      else
        format.html { render action: 'new' }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end

解决方案是从carrierwave文件中获取文件名,如下所示:

  def create
    @photo = Photo.new(params[:photo])
    @photo.user_id = current_user.id


    respond_to do |format|
      if @photo.save
        format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
        format.json { render action: 'show', status: :created, location: @photo }
      else
        format.html { render action: 'new' }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end
  def create
    @photo = Photo.new(params[:photo])
    @photo.user_id = current_user.id

    @photo.name =  @photo.image.file.filename if @photo.name == ""

    respond_to do |format|
      if @photo.save
        format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
        format.json { render action: 'show', status: :created, location: @photo }
      else
        format.html { render action: 'new' }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end