Render Filters
Render filters allow us to override the default rendering that Streamlined does when a CRUD action finishes running. They basically tell us where to go after performing an action. For example, to redirect to the list view instead of back to the show view after creating a new record:
class AddressController < ApplicationController
layout 'streamlined'
acts_as_streamlined
render_filter :update, :success => Proc.new { redirect_to :action => 'list' }
end
The statement above says that after the update action runs successfully, the user should be redirected to the list view. What if we also want to do something if the update action fails, perhaps due to a validation error? We can specify a filter for that as well:
class AddressController < ApplicationController
layout 'streamlined'
acts_as_streamlined
render_filter :update, :success => Proc.new { redirect_to :action => 'list' },
:failure => Proc.new { render :text => 'something bad happened', :status => :bad_request }
end
The code inside the Proc is called in the context of the controller action itself. This means that pretty much anything that can be done inside a controller action can also be done inside the Proc (assigning instance variables, calling methods on a model, etc).
Multiple render filters
Multiple render filters can be used on the same controller:
class AddressController < ApplicationController
layout 'streamlined'
acts_as_streamlined
render_filter :update, :success => Proc.new { redirect_to :action => 'list' },
:failure => Proc.new { render :text => 'something bad happened', :status => :bad_request }
render_filter :edit, :success => Proc.new { render :action => 'custom_edit_form' },
:failure => Proc.new { render :text => instance.errors.full_messages.join('<br/>') }
render_filter :show, :success => Proc.new { render :action => 'custom_show' }
end
Passing a method symbol
If the render filter is especially complex, we can pass a method symbol instead of a Proc and the filter will invoke that method:
class AddressController < ApplicationController
layout 'streamlined'
acts_as_streamlined
render_filter :update, :success => :render_custom_list
def render_custom_list
@some_var = 'some value'
redirect_to :action => 'list'
end
end
