2013-10-06 99 views
0

爲了概括我的問題,我使用了一個返回可迭代對象的API。其中那些是每個對象的id。我的控制器看起來是這樣的:調用視圖控制器中的方法Rails

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 

和定義之前添加索引和自我(例如:self.getList(attr2))並在視圖中的所有變體中調用它:

<%= getList(thing.attr2) %> 

我想知道我在哪裏錯了。我另外嘗試添加helper_method行,因爲我在幾個文檔中閱讀,但它不會識別它。此外,這是否是正確的方式去這種風格明智?很難找到它的參考使我認爲這不是標準的做法。

回答

0

我試圖製作的方法是一個輔助方法,因此需要進入控制器的輔助方法。

相關問題