我使用以下幫助程序方法將一些記錄上傳到Dropbox。對於三種模型(日誌,飛機和方法)中的每一種,我都在做同樣的事情。我想讓這個更幹,只有一次代碼,但我不明白如何以抽象的方式引用模型。DRY在Ruby on Rails中使用模型
有什麼建議嗎?
#------------------------------------------------------------
# Upload entries to Dropbox
#------------------------------------------------------------
def upload_items(items, folder, client)
# Go through each item and upload it to Dropbox
items.each do |item|
if folder == 'logbook'
# Get the file from the database to upload
@logbook = current_user.logbooks.find_by_sync_id(item)
# Upload it
uploaded_file = client.put_file("/logbook/#{item}.json",@logbook.to_json, overwrite = true)
# Reset the updated_flag in the database
@logbook.update_attributes(updated_flag: 0)
elsif folder == 'aircraft'
# Get the file from the database to upload
@aircraft = current_user.aircrafts.find_by_sync_id(item)
# Upload it
uploaded_file = client.put_file("/aircraft/#{item}.json",@aircraft.to_json, overwrite = true)
# Reset the updated_flag in the database
@aircraft.update_attributes(updated_flag: 0)
elsif folder == 'approaches'
# Get the file from the database to upload
@approach = current_user.approaches.find_by_sync_id(item)
# Upload it
uploaded_file = client.put_file("/approaches/#{item}.json",@approach.to_json, overwrite = true)
# Reset the updated_flag in the database
@approach.update_attributes(updated_flag: 0)
end
end
end
的Ruby 1.9.3,Rails的3.2.8
這看起來太棒了。儘管我在'resource = current_user'這一行發生了一個錯誤。 * NoMethodError(未定義方法'logbook'爲#<用戶:0x007fcd7bcd81d8>): app/helpers/sync_helper.rb:118:在'public_send'* –
我的錯誤,文件夾「日誌」需要符號:日誌,所以.pluralize會照顧你。固定在上面。 –
我也是這麼認識的。 :) 謝謝! –