我有用於將當前的用戶到新創建的對象的簡單模塊:Ruby on Rails - 一個模型可以看到模塊,另一個模型不能?
module AttachUsers
def create_with_author(author, params)
created = new(params)
created.author = author
created.save
created
end
def create_with_author_and_editor(author, params)
created = new(params)
created.author = author
created.lasteditor = author
created.save
created
end
end
該模塊被直接lib目錄下保存爲attach_users.rb。
到目前爲止,我已經嘗試過使用這個模塊和兩個模型。它工作得很好,其中第一(評論)的模型,但是第二(頁)返回錯誤信息
undefined method `create_with_author_and_editor'
我已在在我的每個型號的頂部如下:
extend AttachUsers
我
@comment = @post.comments.create_with_author(current_user, params[:comment])
並在頁面控制器是這樣的::
在評論控制器這樣使用它任何人都可以看到爲什麼它可能無法正常工作?這是我第一次嘗試使用模塊,很抱歉,如果它是顯而易見的。
任何意見讚賞。
感謝
哪裏模塊保存? – Matchu 2010-01-21 23:05:35
直接在lib目錄下 – Dan 2010-01-21 23:11:19