Lightning talk

882 views

Published on

LT feita apresnetada no Rock and Rails II

  • Be the first to comment

  • Be the first to like this

Lightning talk

  1. 1. ActionController::Responder Responders na sua aplicação Rails
  2. 2. Responders são responsavéisporExpor os resources para diferentes MIMErequestes"MIME types" são usados para identificar otipo de informação que um arquivo contém
  3. 3. Controllers gigantesclass ProjectsController < ApplicationControllerdef index @projects = Project.all respond_to do |format| format.html # index.html.erb Letras pequenas, sinal de format.json { render json: @projects } coisa ruim endenddef show @project = Project.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @project } endenddef new @project = Project.new respond_to do |format| format.html # new.html.erb format.json { render json: @project } endend
  4. 4. Preciso enxugar meus controllersclass ProjectsController < ApplicationController before_filter :find_project, :only => [:show, :edit, :update, :destroy]] respond_to :html, :jsondef index @projects = Project.all respond_with @projectsenddef show respond_with @projectenddef new @project = Project.new respond_with @projectenddef editend
  5. 5. Condicionais
  6. 6. KEEP CALM and WRITE TESTSResponders são personalizavéis app/responders/application_responder.rb app/responders/create_responder.rb app/responders/update_responder.rbgem responders (github.com/plataformatec/responders) FlashResponder rails generate responders:install
  7. 7. Actions create e updatedef create @project = Project.new(params[:project]) respond_to do |format| if @project.save format.html { redirect_to @project, notice: Project was successfully created. } format.json { render json: @project, status: :created, location: @project } else format.html { render action: "new" } format.json { render json: @project.errors, status: :unprocessable_entity } end end enddef update @project = Project.find(params[:id]) respond_to do |format| if @project.update_attributes(params[:project]) format.html { redirect_to @project, notice: Project was successfully updated. } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @project.errors, status: :unprocessable_entity } end end end
  8. 8. Perimta que o responder cuide da lógica dos"MIME" e alguns condicionaisbefore_filter :find_project, :only => [:show, :edit. :update, :destroy]def create @project = Project.new(params[:project]) respond_with(@project, :location => @project)end def update @project.update_attributes(params[:projects]) respond_with(@project, :location => @projects) end
  9. 9. github.com/gilgomespObrigado! @gil27 facebook.com/gilgomes27 www.gilgomes.com.br

×