首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何传递操作的返回值以查看?

如何传递操作的返回值以查看?
EN

Stack Overflow用户
提问于 2019-11-18 14:09:24
回答 1查看 267关注 0票数 0

我花了很多时间,还在尝试如何将操作的结果(返回)传递给我的视图。关于文档,我在概念文件夹中创建了单元格、操作和查看文件夹。我正在使用搜索应用程序,这是我的手机/show.rb

代码语言:javascript
复制
module Search::Cell
  class Show < Trailblazer::Cell
  end
end

这是view/show.rb

代码语言:javascript
复制
<h1> Hello! </h1>
#how to pass result here?
<a href="/search">Return back</a>

我的操作/展示.

代码语言:javascript
复制
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

和search_controller.rb

代码语言:javascript
复制
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

我应该使用哪个变量或方法来传递操作的结果(hello_world!方法来查看?我尝试了不同的东西(听说过一些关于ctx变量,也尝试过实例变量,比如在普通的rails应用程序中),并且用pry进行了大量的调试,但没有解决它。求你救救我!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-18 16:35:42

根据文档的说法,你似乎有两个选择。

  1. 将操作结果作为“模型”传递,并通过单元格模板中可访问的模型变量访问它。
代码语言:javascript
复制
    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

然后在模板中,假设您使用的是ERB:

代码语言:javascript
复制
    <h1> Hello! </h1>
    The result is <%= model %>
    <a href="/search">Return back</a>
  1. 在上下文中传递操作结果,并通过单元格类中定义的方法访问操作结果。
代码语言:javascript
复制
    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

然后在你的牢房课上:

代码语言:javascript
复制
    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

然后在模板中,假设您使用的是ERB:

代码语言:javascript
复制
    <h1> Hello! </h1>
    The result is <%= my_result %>
    <a href="/search">Return back</a>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58916446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档