我的應用程序的結構如下:的Rails 3:刷新部分與第二級協會
class Study < ActiveRecord::Base
has_many :topics
class Topic < ActiveRecord::Base
has_many :references
我需要訪問我的研究模型(study.topic.references)的節目頁面上的引用。不過,我需要根據(比方說)單選按鈕選擇主題來檢索引用。所以當用戶點擊一個主題時,我需要通過AJAX檢索特定主題的引用。
因此,我寫了下面的方法在我的研究模型:
class Study < ActiveRecord::Base
def self.topic_references(topic_id)
Reference.where(:topic_id => topic_id)
end
我訪問我的研究控制器顯示上述方法如下:
class StudiesController < ApplicationController
@references= Study.topic_references(params[:topic_id])
,我打算有一個遙控器表單將訪問上述方法並檢索相應的引用。是這樣的:
- form_tag topic_references, :id=>"references_form", :method => 'get' do
= text_field_tag :topic_id, params[:topic_id], :value=>268
= submit_tag "Get references"
正如我傳遞然而,當在視圖訪問@references主題ID(以上268值)的值的初始測試仍然是空的。請幫助我理解我做錯了什麼,我該如何做到這一點?
嘗試使用'PARAMS [:references_form] [:topic_id]',而不是'PARAMS [:topic_id]' –
@kishie,這給了我: 你有一個零對象時,你沒想到吧! 您可能預期了Array的一個實例。 評估結果爲零時發生錯誤。[] – rgoraya
提交表單後發生錯誤嗎?在'params [:references_form] [:topic_id]'後面加上'if params [:references_form]'' –