Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 3:使用二级关联重新加载部分_Ruby On Rails_Ruby_Ajax_Ruby On Rails 3_Forms - Fatal编程技术网

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"