2009-10-28 3 views
0

나는 기존 레코드의 여러 형태의 한 페이지가있는 경우 :한 페이지에 여러 양식을 처리하는 방법은 무엇입니까?

selections_controller :

index.html.haml 다음

- for selection in @selections 
    - form_for selection, :method => :put do |form| 
    = form.collection_select :user_id, @current_account.users, :id, :full_name 

과이 제출하기로 업데이트 행동. rb

def update 
    selection = Selection.find(params[:id]) 
    if selection.update_attributes(params[:selection]) 
    flash[:notice] = "Save!" 
    redirect_to selections_path 
    else 
    flash[:errors] = "Errors" 
    render :index 
    end 
end 

어떻게하면 좋을까요? 같은 페이지에 여러 양식이있는 경우 오류 메시지가 표시됩니다. 양식 중 하나에 대한

selection.errors.on(:user_id) 

: 즉 내가 사용하려면?

답변

1

일반적으로 error_msg_for 도우미를 사용하려고합니다.

= error_messages_for object 

그러나 당신이해야 할 일은 signle 업데이트를 기반으로 여러 가지 양식을 렌더링하기 때문입니다.

첫 번째 업데이트 작업은 @selections를 다시 채워야하며 뷰에 사용 가능한 업데이트를 인스턴스 변수로 사용할 수 없도록 선택해야합니다.

def update 
    @selection = Selection.find(params[:id]) 
    if @selection.update_attributes(params[:selection]) 
    flash[:notice] = "Save!" 
    redirect_to selections_path 
    else 
    @selections = Selection.find .... 
    flash[:errors] = "Errors" 
    render :index 
    end 
end 

다음은이 정보를 양식에 입력하십시오.

index.html.erb

- for selection in @selections 
    - form_for selection, :method => :put do |form| 
    = error_messages_for form.object if form.object.id = @selection.id 
    = form.collection_select :user_id, @current_account.users, :id, :full_name 
+0

나는이 오류가 무엇입니까 : @ # <선택은 : 0x0000010276a000>은 '인스턴스 오류에 해당 무엇을 줄 변수 이름 – Cameron

+0

으로 사용할 수 없습니다. – EmFi

+0

error_messages_for @selection을 사용하여 문제를 해결했다고 생각합니다. – Cameron

관련 문제