Carrierwave不在db上保存文件

时间:2014-10-17 13:38:21

标签: jquery ruby-on-rails upload carrierwave

我一直试图将头像上传到我的项目模型,但没有成功。

这是我的观点:

.inbox-body.light-panel
  %span.blue-light-title Look and feel for your projects
  #audit_trail_container
    = form_tag set_project_logo_project_path(project), :method=>:POST do
          .form-control-group
            .separator-div
              .fileuploadadvanced.fileuploadadvanced-new{"data-provides" => "fileuploadadvanced"}
                %span.btn.btn-white.btn-file
                  %span.fileuploadadvanced-new
                    %i.fa.fa-paper-clip
                    Select file
                  %span.fileuploadadvanced-exists
                    %i.fa.fa-undo
                    Change
                  =file_field_tag :avatar, :class => "upload"
                %span.fileuploadadvanced-preview
                %a.close.fileuploadadvanced-exists{"data-dismiss" => "fileuploadadvanced", href: "#"}
      = link_to "Save", "#", :class=> 'btn submit-btn btn-cl-info set_project_logo', :data=> {:project=> project.id}

进入这个jquery:

set_project_logo_action = function() {
    return $(".set_project_logo").on('click', function(event) {
        var checked, project_id, selected, type, uploaded_image;
        project_id = $(this).data('project');
            selected = $("#avatar").val();
            return $.post('/projects/' + project_id + "/set_project_logo", {
                selected: selected,
                type: "file"
            }, function(data) {
                if (data.success) {
                    return document.location.reload(true);
                } else {
                    return alert(data.error);
                }
            });
        } else {
            alert("Select an image to become the logo of the project");
            return event.preventDefault;
        }
    });
};

在project_controller中使用此方法:

def set_project_logo
    set_project
    @project = @project.set_project_logo(params)
    if @project.save
      render :json => {:success => true}
    else
      render :json => {:error => "There was a problem setting the logo."}
    end
  end

,最后在模型上使用此方法:

def set_project_logo(params)
    if params[:selected].present? and params[:type].present?
        self.avatar = params[:selected]
    end
   self
end

在我的模特上,我也有一行:mount_uploader :avatar, ProjectAvatarUploader

它执行的行self.avatar = params[:selected],但是params[:selected]它是“c:\ fakepath ..”。所以我猜我错过了什么,因为在s3上没有任何东西可以保存在db上。

任何帮助?

0 个答案:

没有答案