0

Image 모델과 관련된 간단한 아티클 모델이 있습니다. 관계는 현재 일대일로 적용됩니다 (나중에 변경 될 수 있지만 현재는 괜찮습니다).레일 : 선택적 중첩 이미지 허용; 나중에 추가 허용 nil

처음부터 기사를 만들고 이미지를 추가하면 모든 것이 잘 작동합니다. 여기 잡기가 있습니다 : 이미지를 생성 할 때 선택 사항으로하고 싶습니다. 그러나 나중에 옵션으로 을 유지하고 나중에 이미지를에 추가하십시오.

그러나 편집 작업을 통해 어떻게 처리해야할지 모르겠습니다. 나는이 시도했다 : 새로운 관련 이미지를 저장하지 못했습니다 관리자 : ArticlesController 번호 편집

액티브 :: RecordNotSaved : 결과

def edit 
    @article = Article.find(params[:id]) 
    if @article.image.nil? 
     @article.image = Image.new 
    end 
    render 'articles/edit' 
    end 

....

<%= f.fields_for :image do |image| %> 
    <div class="form-group"> 
     <%= image.label :image, "Article image" %><br/> 
     <%= image_tag(@article.image.path.thumb.url) %> 
     <%= image.file_field :path %> 
    </div> 

    <div class="form-group"> 
     <%= image.label :caption %> 
     <%= image.text_field :caption, class: 'form-control' %> 
    </div> 

    <div class="form-group"> 
     <%= image.label :credits %> 
     <%= image.text_field :credits, class: 'form-control' %> 
    </div> 
    <% end %> 

가 어떻게 나중에 편집 양식을 통해 추가 할 수있는 옵션 중첩 된 이미지를 수행 할 수 있습니다

양식은 현재 다음과 같습니다?

감사합니다.

답변

0

레일스의 단순함은 절대로 나를 놀라게하지 않습니다. Can't update my nested model form for has_one association

에서 영감을 ...

def edit 
    @article = Article.find(params[:id]) 
    @image = @article.image || @article.build_image 
    render 'articles/edit' 
    end 

: 저는 여기에 한 줄 (@image = ...)를 추가하여 해결 방법

관련 문제