ActiveRecord::RecordNotFound in UsersController#update

Posted

技术标签:

【中文标题】ActiveRecord::RecordNotFound in UsersController#update【英文标题】: 【发布时间】:2018-01-30 21:00:46 【问题描述】:

enter image description here我在创建一个只显示电子邮件地址和密码的帐户后添加了一个页面来完成他的个人资料,所以我们发现自己在完成他的个人资料的页面上,将照片添加到他的帐户......一切如果我正确填写字段并上传照片,我可以验证并更新我的个人资料,问题就来了,当我忘记填写必填字段(例如:地址或名字......)并且我已经上传了一个照片,所以我有一个渲染告诉我我忘记了字段,当我完成它们并验证时,这是 cloudinary 犯了一个错误

找不到 'id'=18 的 Attachinary::File

参数:

<ActionController::Parameters "utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"TaAxCldr0Hi2r81Oat3i9ERK8TWapiICbCeyf7J3oR6UDVSnqEoNsOjcJ6ms9Ms8MOPlYUc7mKL9A4sp+Hg4IQ==", "user"=><ActionController::Parameters "last_name"=>"fezfzgz", "first_name"=>"gzrgrzg", "birthday(1i)"=>"2013", "birthday(2i)"=>"8", "birthday(3i)"=>"22", "gender"=>"homme", "website"=>"zesvzzeze", "phone_number"=>"102030405", "avaibility"=>"false", "description"=>"", "address"=>"zregregreg", "zip_code"=>"13210", "city"=>"france", "photos"=>["[\"id\":18,\"public_id\":\"j3b5qdc90jj77ygib26f\",\"version\":\"1503409614\",\"format\":\"jpg\",\"resource_type\":\"image\",\"path\":\"v1503409614/j3b5qdc90jj77ygib26f.jpg\"]"], "videos"=>[""], "voice_attribute"=>"grave", "voices"=>[""] permitted: false>, "commit"=>"Save", "controller"=>"users", "action"=>"update", "id"=>"18" permitted: false>

当前用户:

 #<User id: 18, email: "davqcze@freel.fr", created_at: "2017-08-22 13:46:21", updated_at: "2017-08-22 13:46:21", first_name: "gzrgrzg", last_name: "fezfzgz", birthday: nil, website: "zesvzzeze", role: "actor", gender: "homme", phone_number: "102030405", voice_attribute: "grave", description: "", avaibility: false, address: "zregregreg", zip_code: "52458", city: "france">

用户控制器:

class UsersController < ApplicationController
  def index
    @users = User.all
  end

  def edit
    @user = User.find(params[:id])
  end

  def show
    @user = User.find(params[:id])
    @booking = Booking.new
  end

  def update
    @user = current_user
    if current_user.DA?
      if @user.update(user_params_da)
        redirect_to user_path(current_user)
      else
        render :edit
      end
    else
      if @user.update(user_params)
        redirect_to user_path(current_user)
      else
        render :edit
      end
    end
  end


  def user_params
      params.require(:user).permit(
        :first_name, :last_name, :phone_number, :address, :city, :birthday,
        :website, :avaibility, :description, :zip_code, :gender, :voice_attribute,
        voices: [], videos: [], photos: []
      )
  end
  def user_params_da
      params.require(:user).permit(
        :first_name, :last_name, :phone_number, :address, :city, :birthday,
        :website, :description, :zip_code, :gender
        )
  end
end

用户模型:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise         :database_authenticatable, :registerable,
                 :recoverable, :rememberable, :trackable, :validatable
  has_many       :bookings
  has_many       :projects

  has_attachments :photos, maximum: 10 #limiting upload to 10 units
  has_attachments :videos, maximum: 10 #limiting upload to 10 units
  has_attachments :voices, maximum: 10 #limiting upload to 10 units


  validates :first_name, presence: :true, on: :update, unless: :devise?
  validates :last_name, presence: :true, on: :update, unless: :devise?
  validates :zip_code, presence: :true, on: :update, unless: :devise?
  validates :address, presence: :true, on: :update, unless: :devise?
  validates :city, presence: :true, on: :update, unless: :devise?
  validates :phone_number, presence: :true, on: :update, unless: :devise?
  validates :gender, presence: :true, on: :update, unless: :devise?
  validates :birthday, presence: :true, on: :update, unless: :devise?
   with_options if: :actor? do |actor|
    actor.validates_inclusion_of :avaibility, :in => [true, false], on: :update
    actor.validates :voice_attribute, presence: :true, on: :update
    actor.validates :voice_attribute, presence: :true, on: :update, unless: :devise?
  end

  enum voice_attribute: [:grave, :moyen, :aigu]
  enum gender: [:femme, :homme]
  enum role: [:DA, :actor]


  private

  def devise?
    email_changed? || encrypted_password_changed?
  end
end

https://preview.ibb.co/fxVW45/Capture_du_2017_08_22_23_52_21.png

【问题讨论】:

可以添加错误图片吗? 似乎问题出在Attachinary 配置中没有?你能分享用户模型验证或完整的堆栈跟踪吗? 我只是添加了用户模型和错误图片;-) 【参考方案1】:

我遇到了同样的问题,我在新视图中找到了解决方案

<%= f.input :photo_cache, as: :hidden %> 

在模型中

has_attachment :photo_cache

并且还把 :photo_cache 放在我控制器的强参数中

【讨论】:

以上是关于ActiveRecord::RecordNotFound in UsersController#update的主要内容,如果未能解决你的问题,请参考以下文章