2010-06-10 37 views
4

我有了一個控制器,如果條件:如果在rails中的控制器中有if語句,我應該在哪裏放置respond_to?

def update 
    @contact_email = ContactEmail.find(params[:id]) 

    if @contact_email.update_attributes(params[:contact_email]) 
     flash[:notice] = "Successfully updated contact email." 
     redirect_to @contact_email 
    else 
     render :action => 'edit' 
    end 
    end 

我在哪裏把respond_to代碼塊:

respond_to do |format| 
    format.html {} 
    format.json {render :json =>@contact_email} 
end 

回答

7
def update 
    @contact_email = ContactEmail.find(params[:id]) 
    ok = @contact_email.update_attributes(params[:contact_email]) 

    respond_to do |format| 
    format.html { 
     if ok 
     flash[:notice] = "Successfully updated contact email." 
     redirect_to @contact_email 
     else 
     render :action => 'edit' 
     end 
    } 
    format.json { 
     if ok 
     render :json =>@contact_email 
     else 
     ... 
     end 
    } 
    end 
end 
相關問題