我正在尝试使用Carrierwave上传一个文件,其中包含一个与Carrierwave模型分开的表单和控制器。
我的上传者LogoUploader已安装在设置模型的:logo
属性上。
class Setting < ActiveRecord::Base
validates :name, presence: true, uniqueness: true
mount_uploader :logo, LogoUploader
end
迁移:
class AddLogoToSettings < ActiveRecord::Migration
def change
add_column :settings, :logo, :string
end
end
我在views/feedback/configure.html.erb
处有一个表单,用于通过FeedbackController
处理图片上传:
<%= form_tag({:action => 'save_configuration'}, id: 'form-save') do %>
<div class="form-group">
<%= file_field_tag "settings[cac_hlogo]" %>
<%= image_tag(@cac_hlogo.logo.url) if @cac_hlogo.logo? %>
</div>
<div class="form-actions">
<%= submit_tag t('save').titlecase, class: 'btn btn-primary' %>
</div>
<% end %>
FeedbackController:
class FeedbackController < ApplicationController
def configure
@cac_hlogo = Setting.find_or_create_by(name: 'cac_hlogo')
end
def save_configuration
cac_hlogo = Setting.find_or_create_by(name: 'cac_hlogo')
cac_hlogo.logo = params[:settings][:cac_hlogo]
respond_to do |format|
if cac_hlogo.save
format.html { redirect_to configure_feedback_index_path, notice: 'Configuration Saved.' }
else
format.html { redirect_to root_path, notice: 'There was a problem saving configuration.'}
end
end
end
end
我允许在:logo
:
SettingsController
属性
class SettingsController < ApplicationController
before_action :authenticate_user!
load_and_authorize_resource
def setting_params
params.require(:setting).permit(:logo)
end
end
由于某种原因,文件从未实际上传,我不明白为什么。我在表单中传递的参数似乎没问题,但实际上没有上传任何内容。我错过了什么?注意:我正在使用本地存储,并且应用程序中的所有其他Carrierwave上传器都可以正常工作。
答案 0 :(得分:0)
解决方案是将enctype: "multipart/form-data"
添加到form_tag
,如下所示:<%= form_tag({:action => 'save_configuration'}, enctype: "multipart/form-data", id: 'form-save') do %>