2013-01-08 3 views
0

반복되는 응용 프로그램 형태로, I했습니다 다중 선택에 대한 다음 코드를레일 collection_select 필드 항목 내 레일에서

<div class="field"> 
    <%= f.label :frameworks %><br /> 
    <%= f.collection_select :framework_ids, Framework.all, :id, :name, {}, {:multiple => true} %> 
</div> 

그것은 창조에서 잘 작동하고 올바르게 편집에 previowsly 선택 프레임 워크를 보여줍니다 전망.

하지만 업데이트 된 다른 필드를 제출하면 데이터베이스의 프레임 워크 항목이 반복됩니다.

예를 들어 데이터베이스 "framework1", "frameworks2", "framework1", "frameworks2"를 업데이트 한 후 "framework1", "frameworks2"를 선택한 경우 한 번 더 업데이트하면 다음과 같이됩니다. "프레임 워크 1", "프레임 워크 2", "프레임 워크 1", "프레임 워크 2", "프레임 워크 1", "프레임 워크 2"

그래서 그것을 방지하려면 어떻게해야합니까?

편집 : 컨트롤러는 여기에 있습니다 : 내가 할로 numImages :

@component = Component.find(params[:id]) 

    respond_to do |format| 
     if @component.update_attributes(params[:component]) 
      @component.update_attribute(:numImages, @component.assets.size) 
      @component.save 

      format.html { redirect_to @component, notice: 'Component was successfully updated.' } 
      format.json { head :no_content } 
     else 
      format.html { render action: "edit" } 
      format.json { render json: @component.errors, status: :unprocessable_entity } 
     end 
    end 

그런데, 업데이트 할 올바른?

+1

는 컨트롤러 측 코드를 제공하시기 바랍니다, 문제는 내가 컨트롤러 부분을 추가 한 뷰 페이지 – Salil

+0

에 존재하지 존재한다. – user1573607

답변

0

numImages 업데이트 (하위 질문)에 대해서는 컴포넌트 모델에서 before_update 메소드를 사용하는 것이 좋습니다.

before_update :set_numimages 

    def set_numimages 
    numImages = assets.size 
    end 

또한, 당신이 전화하는거야 update_attributes, update_attribute 및 @Component에 저장. 세 가지 저장 작업이 호출됩니다. 나는 문제가 지속되면이로 변경하고 볼 게 좋을 것 :

@component = Component.find(params[:id]) 

respond_to do |format| 
    if @component.update_attributes(params[:component]) 
    format.html { redirect_to @component, notice: 'Component was successfully updated.' } 
    format.json { head :no_content } 
    else 
    format.html { render action: "edit" } 
    format.json { render json: @component.errors, status: :unprocessable_entity } 
    end 
end