我试图提供一个接口来从电子表格文件“预加载”一个表。听起来很简单,但我正在恶作剧,作为一个完整的新手。我已经使用action_view helper asset_path在资产预编译之后找到文件,但似乎我没有正确包含helpers,因为我一直在为“#”取得“NoMethodError:未定义方法`asset_path'”。这里是我的模型代码:Rails:在模型中使用roo来加载表,不断得到:NoMethodError:未定义的方法`asset_path'
require 'rubygems'
require 'roo'
require 'action_view'
class Item < ActiveRecord::Base
include ActionView::Helpers
validates :description, presence: true, length: { maximum: 240 }
validates :category, presence: true
has_many :interests
has_many :members, through: :interests
end
def populate_items_table
from_file = asset_path("item_listing.ods")
original_list = Openoffice.new(from_file)
original_list.default_sheet = original_list.sheets.first
headers = original_list.row(1)
...
end
我将不胜感激,如果有人能指出我要去的地方错在这里。另外,我是在“Rails方式”中讨论这个问题吗?这种代码应该放在模型还是其他地方?我在其他地方猜测,否则适当的帮手可能已经被定义了?
这里堆栈上也有类似的问题,例如, 1,但答案似乎没有任何不同,我在做什么。
@shishir:这里是包括特定的模块当所述堆栈跟踪的建议:
ERROR["test_should_reload_items_table", ItemsControllerTest, 2016-03-22 08:43:53 +0000] test_should_reload_items_table#ItemsControllerTest (1458636233.20s) NoMethodError: NoMethodError: undefined method
asset_path' for #<ItemsController:0x000000097cb308> app/models/item.rb:14:in
populate_items_table' app/controllers/items_controller.rb:67:inreload' test/controllers/items_controller_test.rb:53:in
block in ' app/models/item.rb:14:inpopulate_items_table' app/controllers/items_controller.rb:67:in
reload' test/controllers/items_controller_test.rb:53:in `block in '
我以前试过,并得到“ActionView :: Template :: Error:未定义的方法错误'为零:NilClass”。我认为通过包括所有帮助者,我更可能包含更具体模块所依赖的所有东西? – FBtLL
可以将堆栈跟踪粘贴到nil类的错误? – Shishir
我的歉意,我冲进了那个回应。 ActionView :: Template :: Error没有关系,我试图在其他领域取得进展,并且这个错误与尚未构建的东西有关......我现在已经发布了来自模型代码测试的更正跟踪,它仍然在报告undefined_method。 – FBtLL