Skip to content

Instantly share code, notes, and snippets.

@ozgun
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save ozgun/9dc43b3e70f0717bc39d to your computer and use it in GitHub Desktop.

Select an option

Save ozgun/9dc43b3e70f0717bc39d to your computer and use it in GitHub Desktop.
responders, Rails 4.2, responders gem

responders, Rails 4.2, responders gem 2.0.2

responders gem'i ile gelen en.yml dosyasında update/alert ve create/alert için mesajlar commentlenmiş durumda, o yüzden aşağıdaki gibi bir flash.en.yml dosyası oluşturulup projenin config/locales/ dizinine kopyalanır.

en:
  flash:
    actions:
      create:
        notice: '%{resource_name} was successfully created.'
        alert: '%{resource_name} could not be created.'
      update:
        notice: '%{resource_name} was successfully updated.'
        alert: '%{resource_name} could not be updated.'
      destroy:
        notice: '%{resource_name} was successfully destroyed.'
        alert: '%{resource_name} could not be destroyed.'
    anodes:
      create:
        notice: "Custom create message."
        alert: "Custom  creation failed message!"
      destroy:
        notice: "Custom destroy notice message."
        alert: "Custom alert message"

Örnek bir controller aşağıdaki gibidir:

class Admin::FeedsController < ApplicationController

  responders :flash # Bu olmazsa flash mesajlar set edilmez!
  respond_to :html

  def index
    @feeds = Feed.includes([:app]).id_desc.page(params[:page])
    respond_with @feeds
  end

  def create
    @feed = Feed.new(feed_params)
    @feed.save
    respond_with(:admin, @feed)
  end

  def update
    @feed.update(feed_params)
    respond_with(:admin, @feed)
  end

  def destroy
    @feed.destroy
    respond_with(:admin, @feed)
  end

end

respond_with ile custom location

respond_with [:members, @member], location: -> { members_profile_path }

Custom Responder

Custom responder kullanmak icin ilgili controller asagidaki gibi olmali:

class Payments::CardsController < ApplicationController
  self.responder = MyModule::CustomServiceResponder
  respond_to :html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment