Ruby on rails Rails 3:使用二级关联重新加载部分
我的申请结构如下:Ruby on rails Rails 3:使用二级关联重新加载部分,ruby-on-rails,ruby,ajax,ruby-on-rails-3,forms,Ruby On Rails,Ruby,Ajax,Ruby On Rails 3,Forms,我的申请结构如下: class Study < ActiveRecord::Base has_many :topics class Topic < ActiveRecord::Base has_many :references 作为初始测试,我正在传递topicid的值(上面的268值),但是,在视图中访问@references时仍然是空的。请帮助我了解我做错了什么,我如何才能做到这一点 您没有显示视图代码,但我猜这是因为其中返回的是对象的集合,而不是单个对象,如下所述: 在控
class Study < ActiveRecord::Base
has_many :topics
class Topic < ActiveRecord::Base
has_many :references
作为初始测试,我正在传递topicid的值(上面的268值),但是,在视图中访问@references时仍然是空的。请帮助我了解我做错了什么,我如何才能做到这一点 您没有显示视图代码,但我猜这是因为
其中
返回的是对象的集合,而不是单个对象,如下所述:
在控制台中,很容易忽略输出中的方括号,例如
[#尝试使用params[:references\u form][:topic\u id]
,而不是params[:topic\u id]
@kishie,这给了我一个提示:当您没有预料到它时,您有一个nil对象!您可能期望得到一个数组实例。计算nil时出错。[]提交表单后会发生这种情况吗?将如果params[:references\u form]
放在params[:references\u form][:topic\u id]
@kishie之后,你建议在哪里使用它?我在我的控制器上使用上述参数作为@references=Study。topic\u references(params[:references\u form[:topic\u id])使用Study.topic\u references(params[:references\u form][:topic\u id])如果参数[:references\u form]
我的问题是参数没有传递给该方法。我需要帮助理解我的表单是否正确指向该方法。(这显然不是因为我得到了错误数量的参数错误)帮助我获得了解决方案。
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"