UsersController#update中的ActiveRecord :: RecordNotFound

时间:2017-08-22 14:07:35

标签: ruby-on-rails ruby activerecord cloudinary

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

  

UsersController中的ActiveRecord :: RecordNotFound#update

     

找不到带有'id'= 18

的Attachinary :: File

params:

<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>

current_user:

 #<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

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我在新视图中添加了一个解决方案

<select class="select-room" data-room="inhouse"></select>
<select class="select-room" data-room="ar_account"></select>

$(document).on('change', '.select-room[data-room="inhouse"]', this.Change1);
$(document).on('change', '.select-room[data-room="ar_account"]', this.Change2);

在模型中

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

并且还将:photo_cache放入我控制器的强力参数中