如何在Rails中访问辅助方法?

时间:2013-07-27 15:01:56

标签: ruby-on-rails

我有一个带有main_controller的rails应用,用于向我的应用主页提供内容。我想访问一组图像,所以我在控制器中创建了以下方法:

def featured_illustrations
    #future implementation: get images where featred == true
    @featured_illustrations << Illustration.find(152)
    @featured_illustrations << Illustration.find(272)
    @featured_illustrations << Illustration.find(275)

    respond_to do |format|
      format.html
      format.json { render :json => @featured_illustrations }
    end
  end
helper_method :featured_illustrations

我有以下帮助:

module MainHelper
  require MainController
  @featured_illustrations = MainController.featured_illustrations
end

featured_images部分中的以下代码:

<div class="featured">
  <% @featured_illustrations.each do |illustration| %>  
   <!-- somc code to print images -->
  <% end %>
</div>

当我尝试查看该页面时出现此错误:

uninitialized constant MainHelper::MainController

我之前没有与佣工合作过,所以我们非常感谢任何指导。感谢。

更新: 我删除了MainHelper但是现在当我尝试加载页面时出现以下错误:

undefined method `each' for nil:NilClass
Extracted source (around line #2):

1: <div class="featured">
2:   <% featured_illustrations.each do |illustration| %>  
3:     <div class="feature-image" id="feature-one">152</div>
4:     <div class="feature-image" id="feature-two">272</div>
5:     <div class="feature-image" id="feature-three">275</div>

对原因的任何想法?

0 个答案:

没有答案