在db或mount_uploader中使用Carrierwave保存对象而不是文件名不起作用

时间:2017-05-18 12:40:59

标签: ruby-on-rails ruby carrierwave

我使用Carrierwave gem上传文件,但我遇到了问题。控制器不会将头像保存在数据库中,尽管该对象来自视图中的参数。如果我从我的模型中删除mount_uploader,Carrierwave将数据库中的对象保存为文件名。我该如何解决?

我的user_controller.rb

  class UsersController < ApplicationController

   before_action :set_user

    def show
    end

   def edit
   end

   def update
    if @user.update(users_params)
     redirect_to user_path(current_user.id)
    else
     puts @user.inspect
     flash.now[:alert]= 'Something wrong'
     render :edit
   end
 end
  def set_user
    @user = User.find(params[:id])
  end

 def users_params
   params.require(:user).permit(:avatar)
  end

  def new
  end


end

我的user.rb模型

 class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, 
      omniauth_providers: [:facebook]

      has_one :account, dependent: :destroy
      mount_uploader :avatar, AvatarUploader
  end

edit.html.haml

.container
  %h1 Edit
  = form_for @user, html: { :multipart => true } do |f|
   = f.label :id
   = f.file_field :avatar
   = f.submit

avatar_uploader.rb

  class AvatarUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :thumb do
    process resize_to_fit: [50, 50]
  end

 def extension_whitelist
   %w(jpg jpeg gif png)
 end
end

1 个答案:

答案 0 :(得分:2)

对象正在数据库中保存,因为您在params中传递完整的头像对象。

安装上传程序后

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
end

并创建&amp;保存新用户

u = User.new
u.avatar = params[:file] # Assign a file like this
u.save!

整个头像对象被分配,然后您可以访问该对象上的各种方法来获取如下字段:

u.avatar.url # => '/url/to/file.png'
u.avatar.current_path # => 'path/to/file.png'
u.avatar_identifier # => 'file.png'

如果您只想在数据库中保存文件名,而不是将avatar传递给用户参数,则只需传递avatar.identifier

相应文件仍将由头像上传者存储

或者您也可以通过创建上传器实例来实现这一点:

uploader = AvatarUploader.new
uploader.store!(my_file)

<强>更新 由于您使用的是Devise,因此必须以不同的方式访问参数。所以User.rb应该是这样的: 在Rails 5中:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :remember_me, :avatar, :avatar_cache, :remove_avatar
end

Rails 4中 而不是模型中的attr_accessible,params必须在Application Controller

中列入白名单
class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:username, :email, :password,
      :password_confirmation, :remember_me, :avatar, :avatar_cache, :remove_avatar) }
    devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:username, :email, :password,
      :password_confirmation, :current_password, :avatar, :avatar_cache, :remove_avatar) }
  end
end