저는 Rails에 처음으로 익숙해 졌기 때문에 여기에 간단한 개념이 있습니다.빌드를 호출하면 중첩 된 속성이 삭제됩니다.
저는 사용자 모델 (Devise 제공)을 보유하고 있으며 각 사용자는 자신의 사진 속성을 가지고 있습니다. 나는 내가 사진을 사용자의 일부로 포함 할 수 있었지만 실제로 사진은 사이트의 핵심 콘텐츠이므로 자신의 테이블로 선호했다. 사진 모델에는 실제 사진 파일을 처리하는 클립 클립 첨부 파일이 있습니다.
다음은 문제입니다. 사진을 사용자로 업로드 할 때 계획대로 작동하지만, 어떤 이유로 사진 업로드 페이지로 돌아 가면 방금 업로드 한 사진이 삭제됩니다. 나는이 코드 라인을 아래로 추적 한 : 나는 그렇게 부르지 않으면
user.build_photo
@photo = @ 업로드에 대한 양식 @ user.photo 때문에 전무 클래스 오류가 발생합니다 존재하지 않지만, 내가 그것을 부르면 이전에 업로드 된 사진이 삭제됩니다. 이상하게 생각됩니다. 왜냐하면 내가 아는 한, 빌드가 아닌 데이터베이스를 변경시키는 create 함수이기 때문입니다. 여기
서버가 표시 내용은 다음과 같습니다는 HTML 사용자 부하로 SettingsController # 지수에 의해 2012-03-08 10시 19분 21초 -0800 처리에 127.0.0.1에 대한 "/ 설정"GET 시작 (0.3ms) SELECT
users
. * FROMusers
WHEREusers
.id
= 6 LIMIT 1 사진 로드 (0.3ms) 선택photos
. * FROMphotos
WHEREphotos
.user_id
= 6 LIMIT 1 (0.2ms) BEGIN [클립] 첨부 파일을 삭제하도록 예약합니다. SQL (0.6ms)에서 삭제photos
WHEREphotos
.id
= 20 [클립] 첨부 파일 삭제 중.
그리고 여기 내 모델과 컨트롤러의 부부의 하나가 이미 존재하는 경우 그 user
에 대한 photo
을 제거합니다 @user.build_photo
를 호출
class SettingsController < ApplicationController
def index
@user = current_user
@photo = @user.build_photo
end
end
<h1>Settings Page</h2>
<%= image_tag @user.photo.the_photo.url(:medium) %>
<%= form_for [@user, @photo], :html => { :multipart => true } do |f| %>
<%= f.file_field :the_photo %>
<%= f.submit %>
<% end %>
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :photo_attribute
has_one :photo, :dependent => :destroy
accepts_nested_attributes_for :photo
end
class PhotosController < ApplicationController
def create
@user = current_user
@photo = @user.create_photo(params[:photo])
redirect_to root_path
end
def update
@user = current_user
@photo = @user.photo
if @photo.update_attributes(params[:photo])
redirect_to settings_path
else
redirect_to settings_path
end
end
def destroy
end
end
Genius, 감사합니다! –