我花了很多时间,还在尝试如何将操作的结果(返回)传递给我的视图。关于文档,我在概念文件夹中创建了单元格、操作和查看文件夹。我正在使用搜索应用程序,这是我的手机/show.rb
module Search::Cell
class Show < Trailblazer::Cell
end
end这是view/show.rb
<h1> Hello! </h1>
#how to pass result here?
<a href="/search">Return back</a>我的操作/展示.
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
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进行了大量的调试,但没有解决它。求你救救我!
发布于 2019-11-18 16:35:42
根据文档的说法,你似乎有两个选择。
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:
<h1> Hello! </h1>
The result is <%= model %>
<a href="/search">Return back</a> 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然后在你的牢房课上:
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:
<h1> Hello! </h1>
The result is <%= my_result %>
<a href="/search">Return back</a>https://stackoverflow.com/questions/58916446
复制相似问题