我的Rails已經變得相當生疏,我開始瘋狂地試圖找出可能非常基本的東西,我看不到樹木的森林。在控制器中調用的Rails方法中類未定義的方法錯誤
我有一個表單,用於控制器的create
操作。當我保存時,我還需要從另一個表中更新一些項目。我想把它全部包裝在一個交易中。我認爲首選是將交易放在模型中。當我這樣做,並嘗試打電話給create
行動時,它錯誤地告訴我,該類沒有任何方法。
equip_swaps_controller.rb
def create
respond_to do |format|
@equip_swap = EquipSwap.new(equip_swap_params)
if @equip_swap.trans_equip_and_save
format.html { redirect_to @equip_swap, notice: 'Equipment transfer was successfully created.' }
else
format.html { render action: 'failure' }
end
end
end
型號equip_swap.rb
def self.trans_equip_and_save
EquipSwap.transaction do
Tool.transfer_equipment(self.to_vehicle, self.items_moved)
self.save
end
end
與所需的方法工具模型
def transfer_equipment(location,ids)
ids.each do |id|
Tool.find(id).update(location: location)
end
end
我認爲調用類的方法將允許它執行的是方法在我的實例EquipSwap
實例@equip_swap
。當我嘗試提交表單並創建新記錄時,它告訴我Class....
沒有trans_equip_and_save
方法。有些事情顯而易見,我錯過了。幫幫我!
我得到一個新的錯誤'未定義的方法'transfer_equipment」爲#<類別:0x007ff28ff9af18>'從線'Tool.transfer_equipment(self.to_vehicle,self.items_moved)'。我是否必須改變這些'self'來顯式調用實例? – Beartech
更改爲'def self.transfer_equipment(location,ids)' – Sajin
@Beartech檢查我編輯的答案 – ashvin