ruby-on-railsrubytrailblazer

How to pass a return value of operation to view?


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!


Solution

  • 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>