Ruby on rails - ROR Carrierwave 不会在数据库中保存文件和 NULL。



载波不保存文件上传到指定的目录,以及在数据库中,它的值是NULL,虽然它创建了一个新的记录。当我试图在文件数据中放置一个validates时,错误消息总是说我在表单中放置一个空白数据,尽管我选择了一个文件,并且文件名显示在按钮旁边。

控制器

class SelfServe::EmployeeFilesController < SelfServe::BaseController 
  def new
    @file = EmployeeFile.new
    @section = section
    render 'self_serve/employees/modal_form'
  end
  def create
    @file = EmployeeFile.new(employee: @employee)
    if @file.save
      flash[:notice] = 'Your file has been successfully uploaded.'
    else
      flash[:error] = 'Failed to upload you file, please check fields with error.'
    end
    @section = section
    @associated_resource = @file
    render 'self_serve/employees/modal_form_submit'
  end
  def edit
    @file = EmployeeFile.find(params[:id])
    @section = section
    render 'self_serve/employees/modal_form'
  end
  def update
    @file = EmployeeFile.find(params[:id])
    if @file.update_attributes(secure_params)
      flash[:notice] = 'Your file has been successfully updated.'
    else
      flash[:error] = 'Failed to save file, please check fields with error.'
    end
    @section = section
    @associated_resource = @file
    render 'self_serve/employees/modal_form_submit'
  end
  def destroy
    @file = EmployeeFile.find(params[:id])
    if @file.destroy
      flash[:notice] = 'File has been successfully deleted.'
    else
      flash[:error] = 'Failed to delete file.'
    end
    @section = section
    render 'self_serve/employees/modal_destroy'
  end
  private
  def section
    'personal_files'
  end
  def secure_params
    params.require(:employee_file).permit(:file)
  end
end

模型(没有验证:file)

class EmployeeFile < ActiveRecord::Base
  belongs_to :employee
  audited associated_with: :employee
  mount_uploader :file, EmployeeFileUploader
  validates :employee, presence: true
end

EmployeeFileUploader

class EmployeeFileUploader < CarrierWave::Uploader::Base
  include CarrierWave::Meta
  storage :file
  process :store_meta => [{md5sum: true}]
  def store_dir
    "uploads/employee/#{model.id}/files"
  end
  # Override the filename of the uploaded files:
  def filename
    @name ||= "#{Digest::MD5.hexdigest(original_filename + Time.now.to_i.to_s)}.#{file.extension}" if original_filename.present?
  end
  def extension_white_list
    %w(ods docx doc xls pdf txt jpg jpeg gif png)
  end
end
视图

<h4>Personal Files</h4>
<% if @employee.files.any?  %>
    <div class="row">
        <div class="col-md-12">
            <%= link_to [:new, :self_serve, :employee, :employee_file], remote: true, class: 'btn btn-sm btn-success pull-right' do %>
            <i class="fa fa-file"></i> Add File</a> 
            <% end %>
        </div>
        <hr/>
    </div>
    <table class="table table-striped">
        <thead class="bg-green-active">
            <th>Filename</th>
            <th>Date Uploaded</th>
            <th></th>
        </thead>
        <tbody>
            <% @employee.files.each do |file| %>
                <tr>
                    <td><%= file.file %></td>
                    <td><%= file.created_at %></td>
                    <td>
                        <%= link_to [:edit, :self_serve, :employee, file], remote: true, class: 'btn btn-xs btn-default' do %>
                        <i class="fa fa-edit"></i> Edit 
                        <% end %>
                        <%= link_to [:self_serve, :employee, file], remote: true, method: :delete, class: 'btn btn-xs btn-default', data: { confirm: 'Are you sure to delete? This action cannot be undone. ' } do %>
                        <i class="fa fa-trash"></i> Delete 
                        <% end %>
                    </td>
                </tr>
            <% end %>   
        </tbody>
    </table>
<% else %>
  <div class="row">
    <div class="col-md-12">
      <%= link_to [:new, :self_serve, :employee, :employee_file], remote: true, class: 'btn btn-success pull-center' do %>
        <i class="fa fa-plus"></i> No files uploaded yet, upload one now.
      <% end %>
    </div>
    <hr/>
  </div>
<% end %>
<% if @file.present? %>
<div class="modal fade" id="file_form_modal" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <%= simple_form_for @file, url: @file.new_record?? self_serve_employee_employee_files_path : self_serve_employee_employee_file_path(@file), :html => {:multipart => true}, remote: true do |f| %>
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title"><%= (@file.new_record?? 'Add' : 'Edit') %> file</h4>
                </div>
                <div class="modal-body">
                    <div id="modal_flash_messages"></div>
                    <div class="row form-group">
                        <div class="col-md-6">
                            <%= f.input :file %>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button>
                    <%= f.button :submit, 'Save changes' %>
                </div>
            <% end %>
        </div>
    </div>
</div>
<% end %>

在创建新记录时,我还有另一个问题。正如你所看到的,我只是使用。save,但如果我使用。update_attributes(secure_params),它会有一个错误在Inspect Element> Network说未定义的方法操纵!

是否将参数列入白名单?这是一个简单的博客帖子控制器。

class PostsController < ApplicationController
  def create
    @post = Post.new(post_params)
    if @memorial.save
      flash[:notice] = "Your memorial was saved."
      redirect_to creator_memorials_path
    else
      render :new
    end
  end
  private
  def post_params
    params.require(:post).permit(:title, :description)
  end
end

相关内容

  • 没有找到相关文章

最新更新