【问题标题】:Carrierwave is not handling file upload when AR backed object is updated更新 AR 支持的对象时,Carrierwave 不处理文件上传
【发布时间】:2014-08-10 12:52:16
【问题描述】:

rails 中的更新操作可以处理使用 Carrierwave 上传的文件吗?完成操作后,我在任何地方都找不到“上传”图像的记录。在 Rails 控制台中,我调用 user.small_cover,然后返回以下内容,看起来并不令人满意:#, @mounted_as=:small_cover>

我的更新动作比较大,所以我只会显示重要的部分

...
elsif a_nonprofit_staff_member_updates_profile?(organization)
  @user.update_columns(user_params)
  flash[:notice] = "You have updated your profile successfully."
  redirect_to user_path(@user.id)
...
def user_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, 
      :organization_id, :bio, :skills, :interests, :position, :user_group, :contact_reason,
      small_cover: [:original_filename] )
end

我尝试只使用 :small_cover 作为属性,但出现错误,没有 Nil 对象的 Name 方法,这对我来说很奇怪,因为该对象不是 nil 并且我没有调用名称、方法.我是否正确打包提交的参数?

当操作正在更新而不是在 Rails 中创建 AR 支持的对象时,如何使用carrierwave成功上传文件?我是不是处理参数不正确

更新: 这是我的用户模型和上传器的相关代码

class User < ActiveRecord::Base

  has_secure_password validations: false
  belongs_to :organization
  has_one :administrated_organization, foreign_key: 'user_id', class_name: 'Organization'
  has_many :sent_messages, class_name: 'PrivateMessage', foreign_key: 'sender_id'
  has_many :received_messages, -> {order('created_at DESC')}, class_name: 'PrivateMessage', foreign_key: 'recipient_id'

  has_many :administrated_projects, through: :administrated_organization, source: :projects

  has_many :volunteer_requests,  class_name: 'VolunteerApplication', foreign_key: 'administrator_id'
  has_many :projects, through: :volunteer_applications, source: :administrator
  has_many :delegated_projects, class_name: "Contract", foreign_key: 'contractor_id'
  has_many :projects, through: :contracts, source: :contractor

  has_many :requests_to_volunteer, class_name: 'VolunteerApplication', foreign_key: 'applicant_id'
  has_many :projects, through: :volunteer_applications, source: :applicant
  has_many :assignments, class_name: "Contract", foreign_key: 'volunteer_id'
  has_many :projects, through: :contracts, source: :volunteer

  has_many :questions

  validates_presence_of :email, :password, :first_name, :last_name, :user_group
  validates_uniqueness_of :email

  mount_uploader :small_cover, SmallCoverUploader

  before_create :generate_token
end

class SmallCoverUploader < CarrierWave::Uploader::Base
end

架构:

ActiveRecord::Schema.define(version: 20140619170102) do

  create_table "contracts", force: true do |t|
    t.integer  "contractor_id"
    t.integer  "volunteer_id"
    t.boolean  "active"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "project_id"
    t.boolean  "dropped_out"
    t.boolean  "complete"
    t.boolean  "incomplete"
    t.boolean  "work_submitted"
  end

  create_table "conversations", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id"
    t.integer  "volunteer_application_id"
    t.integer  "contract_id"
  end

  create_table "organizations", force: true do |t|
    t.string   "name"
    t.datetime "ruling_year"
    t.text     "mission_statement"
    t.string   "guidestar_membership"
    t.string   "ein"
    t.string   "street1"
    t.string   "street2"
    t.string   "city"
    t.integer  "state_id"
    t.string   "zip"
    t.integer  "ntee_major_category_id"
    t.string   "funding_method"
    t.integer  "user_id"
    t.string   "cause"
    t.string   "state_abbreviation"
    t.text     "goal"
    t.string   "contact_number"
    t.string   "contact_email"
    t.string   "budget"
  end

  create_table "private_messages", force: true do |t|
    t.integer  "sender_id"
    t.integer  "recipient_id"
    t.string   "subject"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "project_id"
    t.integer  "conversation_id"
  end

  create_table "projects", force: true do |t|
    t.string   "title"
    t.text     "description"
    t.string   "skills"
    t.string   "causes"
    t.datetime "deadline"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id"
    t.integer  "organization_id"
    t.integer  "estimated_hours"
    t.string   "state"
  end

  create_table "questions", force: true do |t|
    t.integer  "user_id"
    t.string   "title"
    t.text     "description"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.integer  "organization_id"
    t.string   "first_name"
    t.string   "last_name"
    t.string   "email"
    t.string   "interests"
    t.string   "skills"
    t.string   "street1"
    t.string   "street2"
    t.string   "city"
    t.integer  "state_id"
    t.integer  "phone_number"
    t.string   "zip"
    t.boolean  "organization_administrator"
    t.boolean  "organization_staff"
    t.boolean  "volunteer"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "password_digest"
    t.string   "position"
    t.integer  "project_id"
    t.string   "time_zone"
    t.text     "bio"
    t.string   "contact_reason"
    t.string   "organization_role"
    t.boolean  "nonprofit"
    t.string   "type"
    t.string   "user_group"
    t.string   "state_abbreviation"
    t.string   "new_password_token"
    t.integer  "profile_progress_status"
    t.string   "small_cover"
  end

  create_table "volunteer_applications", force: true do |t|
    t.integer  "user_id"
    t.integer  "project_id"
    t.boolean  "accepted"
    t.boolean  "rejected"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "applicant_id"
    t.integer  "administrator_id"
  end

end

上传者

class SmallCoverUploader < CarrierWave::Uploader::Base
end

【问题讨论】:

    标签: ruby-on-rails-4 carrierwave


    【解决方案1】:

    您应该单独尝试:

    @user.small_cover.update(cover_params)
    

    我猜你的关联是belongs_to :small_cover

    您需要使用第二种强参数方法(此处称为cover_params)。这意味着你需要适应params

    【讨论】:

    • small_cover 是采用字符串作为上传文件名的属性。我更新了帖子以提供更多信息
    • 那么您的 CarrierWave 实施模型是什么?您能否向我们展示您的带有 schema.rb 和上传器的用户模型?
    • 我更新了问题。让我知道这是否有帮助。非常感谢您的帮助!
    【解决方案2】:

    我只是想在这里做一个更新,如果我的简短回答不能满足读者的好奇心,我很乐意澄清。

    我基本上只使用了cloudinaryattachinary。这个组合就像一场梦。再见载波!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多