Created
September 2, 2012 14:25
-
-
Save fnando/3599511 to your computer and use it in GitHub Desktop.
Just a refactored controller that uses a custom responder instead of those nasty respond_to blocks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NotesController < ApplicationController | |
self.responder = NotesResponder | |
def create | |
@page = Page.find(params[:page_id]) | |
@note = page.notes.create(params[:note]) | |
respond_with(@note, :location => page_path(@page)) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NotesResponder < ActionController::Responder | |
delegate :t, :flash, :to => :controller | |
def to_html | |
if resource.new_record? | |
flash[:notice] = t("fuck_yeah") | |
else | |
flash[:alert] = resource.errors.full_messages.to_sentence | |
end | |
redirect_to navigation_location | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NotesController < ApplicationController | |
def create | |
@page = Page.find(params[:page_id]) | |
@note = page.notes.create(params[:note]) | |
respond_to do |format| | |
format.html do | |
if @note.new_record? | |
flash[:notice] = t("fuck_yeah") | |
else | |
flash[:alert] = @note.errors.full_messages.to_sentence | |
end | |
redirect_to page_path(@page) | |
end | |
format.json | |
end | |
end | |
end |
tinogomes
commented
Sep 2, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment