From 547e8b3dd8b9f1d34a76116d988d634bcd3c272f Mon Sep 17 00:00:00 2001 From: Jens Ljungblad Date: Fri, 4 Dec 2015 13:44:44 +0100 Subject: [PATCH] Adds readme for passing params from controller to service --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index 183f0001..207e2688 100644 --- a/README.md +++ b/README.md @@ -406,6 +406,36 @@ class ArticlesController < ApplicationController end ``` +#### Passing parameters to the service object + +Sometimes you want to pass additional params to the service object, other that those passed in `resource_params`. In order to do this, you need to pass them along when initializing the service object in the controller: + +```ruby +class ArticlesController < ApplicationController + include Godmin::Resources::ResourceController + + private + + def resource_service + service = super + service.options[:some_param] = params[:some_param] + service + end +end +``` + +You can then access it from the service object: + +```ruby +class ArticleService + include Godmin::Resources::ResourceService + + def some_method + options[:some_param] + end +end +``` + ### Redirecting By default the user is redirected to the resource show page after create and update, and to the index page after destroy. To change this, there are four controller methods that can be overridden: `redirect_after_create`, `redirect_after_update`, `redirect_after_save`, and `redirect_after_destroy`.