2010-06-10 14 views
4

J'ai un contrôleur qui a si condition:où dois-je mettre le respond_to s'il y a une instruction if dans le contrôleur dans les rails?

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 

Où dois-je mettre le bloc respond_to:

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

Répondre

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