0
我創造了一些腳手架來管理音頻剪輯這是要通過索引來組織:在另一個控制器的模型方法中訪問實例變量的正確方法是什麼?
rails generate scaffold Clips name:string
我上傳的所有剪輯到我的文件服務器,並使用自動生成的軌道控制面板將它們添加到數據庫。
現在,我需要能夠訪問他們,所以我增加了一個網址的方法模型:現在
class Clip < ActiveRecord::Base
def self.url
"http://example.file_server.com/audio-clips/#{@id}.mp3"
end
end
,比運行網站本身的控制器,調用這個方法看起來像它輸出一切,但ID ....
class TwilioController < ApplicationController
def index
Twilio::TwiML::Response.new do |r|
@response = r.play Clip.where(name: "root").url
end
render :xml => @response
end
end
輸出:
<Response>
<play>http://example.file_server.com/audio-clips/.mp3</play>
</Response>
我怎樣才能得到這件事情將id
插入到URL字符串中?
因爲關係問題,我最終做了'self'。加入'.first'修復了我的問題。謝謝! – drewwyatt