ActiveRecord::RecordNotFound in UsersController#update



在此处输入图像描述我在创建一个仅指示电子邮件地址和密码的帐户后添加了一个页面来完成他的个人资料,因此我们发现自己在完成他的个人资料的页面上,将照片添加到他的帐户...如果我正确填写字段并上传照片,一切正常,我可以验证并且我的个人资料已更新,当我忘记填写必填字段(例如:地址或first_name..(并且我已经上传了照片时,问题就来了,所以我有一个渲染告诉我我忘记了字段,当我完成它们并验证时, 是云子犯了错误

ActiveRecord::RecordNotFound in UsersController#update

找不到 Attachinary::文件,"id"=18

参数 :

<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

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

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

在模型中

has_attachment :photo_cache

并将:p hoto_cache放在控制器的强参数中

最新更新