从控制器访问辅助方法时未定义的局部变量或方法

时间:2014-09-15 16:08:45

标签: ruby-on-rails ruby ruby-on-rails-4

我创建了一个名为'内部版本的控制器。

我想检查多选对象是否至少包含一个选项。

在我的控制器中我有:

class InternalReleasesController < ApplicationController

  def show

    if params[:run].nil?
      logger.error "Attempt to get trend result without going through the internal_releases_trend_selection_url"
      flash[:no_arguments] = 'You have tried accessing trend results without selecting parameters.'
      redirect_to internal_releases_trend_selection_url

    else 
      all_options = Array.new(params[:run][:category_id])
      missing_selections = validate_arguments params[:run]

      all_options = Array.[]params[:run][:category_id]

      logger.debug "all_options is: #{all_options.class}"
    end

  end
end

我创建了一个简单的帮助方法:

module InternalReleasesHelper

  def validate_arguments multiselect_hash

    answer = Array.new

    multiselect_arr.each do |key, val_arr|
      if val_arr.length==1  # therefore, no selection made in this multiselect- the first arg will always be ""
        answer << key
      end
    end
    answer
  end
end

出于某种原因,我得到了:

undefined method `validate_arguments' for #<InternalReleasesController:0x007faf08bf9f78>

可能导致这种情况的原因是什么?

2 个答案:

答案 0 :(得分:3)

将帮助程序模块 InternalReleasesHelper 包含在 InternalReleasesController

class InternalReleasesController  
include InternalReleasesHelper  
end 

答案 1 :(得分:1)

Helper的方法默认情况下只在Views中可用,因此你应该将你的助手包含在控制器中:

浏览internal_releases_controller.rb文件并插入以下内容:

include InternalReleasesHelper