【发布时间】:2018-12-16 06:20:23
【问题描述】:
我正在尝试编写一个模块,我想用我在控制器中的操作替换该模块。为此,我在我的控制器文件夹中创建了一个名为 test 的模块。我想将我的操作放在哪里,我的控制器操作代码是:
def test
rain_fall_type = "test"
year = ""
compare = params[:compare]
respond_to do |format|
format.html { render json: rain_fall_type }
end
end
我想将此代码放入我的模块代码中我已将此代码添加到我的模块中,其代码为:
module Test
def test
rain_fall_type = "params[:rain_fall_type]
views = params[:views]"
year = ""
compare = params[:compare]
respond_to do |format|
format.html { render json: rain_fall_type }
end
end
end
我正在尝试将其扩展到我的控制器中,因此我将扩展测试放入我的控制器中,但出现此错误:
The action 'test' could not be found for ProductionProductivity7sController
当我从我的模块中删除 def test 并将此代码放入控制器中时:
def test
extend Test
end
我从模块中删除了 def test 并将其更改为:
module Test
rain_fall_type = "params[:rain_fall_type]
views = params[:views]"
year = ""
compare = params[:compare]
respond_to do |format|
format.html { render json: rain_fall_type }
end
end
当我这样做时,我收到了这个错误:
undefined local variable or method `params' for Test:Module
我应该怎么做才能将我的测试操作替换到我的模块中。
【问题讨论】:
标签: ruby-on-rails module controller