Ruby on rails 在视图控制器Rails中调用方法

Ruby on rails 在视图控制器Rails中调用方法,ruby-on-rails,ruby,erb,Ruby On Rails,Ruby,Erb,为了概括我的问题,我使用了一个返回iterable对象的API。其中每个对象都有一个id。我的控制器如下所示: class SearchController < ApplicationController def index @search = API.find(params[:query]) end end <% @search.each do |thing| %> <h2><%= thing.att

为了概括我的问题,我使用了一个返回iterable对象的API。其中每个对象都有一个id。我的控制器如下所示:

class SearchController < ApplicationController
    def index
        @search = API.find(params[:query])
    end
end
    <% @search.each do |thing| %>
        <h2><%= thing.attr2 if thing.attr1 %></h2>
        <%= API.list(thing.attr2) %>
    <% end %>
class SearchController
我的观点是这样的:

class SearchController < ApplicationController
    def index
        @search = API.find(params[:query])
    end
end
    <% @search.each do |thing| %>
        <h2><%= thing.attr2 if thing.attr1 %></h2>
        <%= API.list(thing.attr2) %>
    <% end %>

我已尝试在中添加一种方法

class SearchController < ApplicationController
    def index
        @search = API.find(params[:query])

            def getList(attr2)
                API.list(thing.attr2)
            end   
    end
end
class SearchController
在定义之前添加索引和self(例如:self.getList(attr2)),并在视图中的所有这些变体中调用它:

<%= getList(thing.attr2) %>


我想知道我在这里哪里出错了。我在阅读一些文档时还尝试添加helper_方法行,但它无法识别。此外,这是正确的方式来进行这种风格明智?很难找到它的引用,这让我觉得这不是标准做法。

我试图创建的方法是一个helper方法,因此,需要为控制器使用helper方法