5

if-conditionを持つコントローラーがあります:

  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

response_toブロックはどこに配置しますか:

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

1 に答える 1

9
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
于 2010-06-10T05:14:49.543 に答える