当我设置一个简单的显示所有事物视图的时候,我对开路先锋有困难。
操作
class Thing < ApplicationRecord
class ShowAll < Trailblazer::Operation
include Model
model Thing, :all #why :all is not working here?
def process
end
end
end控制器
class PageController < ApplicationController
def index
run Word::ShowAll
end
end为什么:all不能从db中获取所有的东西,但是:find会通过它的id来获得一个呢?
发布于 2016-11-19 00:35:53
问TRB问题的最佳地点实际上是在Github频道。
我不知道你在哪里找到这个例子,因为它不应该起作用AFAIK :查找是一条捷径,我相信,我从来没有真正使用过它。
您的所有逻辑都应该在process方法中定义。http://trailblazer.to/gems/operation/1.1/api.html#process
尽管如此,尝试在没有分页的情况下获取所有记录是一个非常糟糕的想法,除非您100%确信您的表不会超过几十个记录。除非你知道你没有很大的负担。因此,定义这种捷径是危险的。
发布于 2016-11-19 14:25:31
在那里调用Trailblazer::Model#model只是重写TrailBlazer::Operaration#model!方法的一个快捷方式。所以你似乎想做的是:
class Thing < ApplicationRecord
class ShowAll < Trailblazer::Operation
def model!(params)
Thing.all # add any filtering or pagination here
end
end
end在控制器中调用present而不是run,这样它就可以建立模型,而不调用操作的process方法。
class PageController < ApplicationController
def index
present Word::ShowAll
end
endhttps://stackoverflow.com/questions/40681634
复制相似问题