2016-08-23 39 views
0

我試圖上傳一個Carrierwave的文件與窗體和控制器是分開的模型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

解決的辦法是添加enctype: "multipart/form-data"form_tag像這樣:<%= form_tag({:action => 'save_configuration'}, enctype: "multipart/form-data", id: 'form-save') do %>

相關問題