Carrierwaveはアップロードされたファイルを指定されたディレクトリとデータベースに保存しません。新しいレコードを作成しましたが、その値はNULLです。私はファイルデータにバリデーションを入れようとしたとき、エラーメッセージは常に、ファイルを選択したにもかかわらず空のデータをフォームに入れていて、ファイル名がボタンの横に表示されていることを示しています。ROR CarrierwaveはファイルとNULLをDBに保存しません
コントローラ(用を検証なし:ファイル)
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
モデル
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">×</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で定義されていないメソッド操作がエラーになります!
どのようにリモートとマルチパートをやっていますか? – Swards