0

I've been spent a lot of time and still trying figure out how to pass a result(return) of operation to my view. As regarding documentation I created cell, operation and view folders inside concepts folder. I'm working with Search app, here is my cell/show.rb

module Search::Cell
  class Show < Trailblazer::Cell
  end
end

Here is view/show.rb

<h1> Hello! </h1>
#how to pass result here?
<a href="/search">Return back</a>

My operation/show.rb

require "trailblazer/operation"

module Search::Operation
  class Show < Trailblazer::Operation
    step    :hello_world!
    fail    :log_error

    def hello_world!(options, search_word)
      puts "Hello, Trailblazer!"
      search_word = search_word[:params]
      search_word.downcase!
      true
    end

    def log_error
      p "Some error happened!!"
      true
    end
  end
end

And search_controller.rb

class SearchController < ApplicationController
  def index
    search_word = params[:text]
    if search_word.present?
      Search::Operation::Show.(params: search_word)
      render html: cell(Search::Cell::Show).()
    else
      render html: cell(Search::Cell::Index).()
    end
  end 
end

Which variable or method should I use to pass result from operation (hello_world! method to view? I tried different things (heard some about ctx variable also tried instance variable like in common rails app) and debugging a lot with pry and didn't solve it. Please, help me!

1 Answers1

1

According to the docs, it seems like you have two options.

  1. Pass the result of your operation as the "model" and access it via the model variable that is accessible in the cell's template.
    class SearchController < ApplicationController
      def index
        search_word = params[:text]
        if search_word.present?
          result = Search::Operation::Show.(params: search_word)
          render html: cell(Search::Cell::Show, result)
        else
          render html: cell(Search::Cell::Index)
        end
      end 
    end

And then in your template, assuming you're using ERB:

    <h1> Hello! </h1>
    The result is <%= model %>
    <a href="/search">Return back</a>
  1. Pass the result of your operation within the context and access it through a method defined in the cell class.
    class SearchController < ApplicationController
      def index
        search_word = params[:text]
        if search_word.present?
          result = Search::Operation::Show.(params: search_word)
          render html: cell(Search::Cell::Show, nil, result: result)
        else
          render html: cell(Search::Cell::Index)
        end
      end 
    end

And then in your cell class:

    module Search::Cell
      class Show < Trailblazer::Cell
        def my_result
          #here is the logic you need to do with your result
          context[:result]
        end
      end
    end

And then in your template, assuming you're using ERB:

    <h1> Hello! </h1>
    The result is <%= my_result %>
    <a href="/search">Return back</a>
Jose Diaz
  • 26
  • 1
  • From the first example I receive "The result is #", I inspected it with pry and here is output " "array"}, @mutable_options={}, @aliases={}>" – Jessie Scinor Nov 18 '19 at 18:02
  • So, in your operation, according to the [docs](http://trailblazer.to/gems/operation/2.0/#state-and-result) you need to write to the `options` object the result of your operation, and then use it wherever you need to. In this case your operation could do something like `options['search_word'] = search_word.downcase` and then in the template `The result is <%= model['search_word'] %>` – Jose Diaz Nov 18 '19 at 18:24
  • In this case empty value of 'search_word': "array"}, @mutable_options={"search_word"=>nil}, @aliases={}> > – Jessie Scinor Nov 18 '19 at 18:42
  • One more question: to make some custom of this output I need to create logic in custom cell method? and then invoke it in view? – Jessie Scinor Nov 18 '19 at 18:50
  • Yes, if you mean presentation logic, you can create a method in the cell and then invoke it in the template – Jose Diaz Nov 18 '19 at 19:00