Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 On Rails 3 - Fatal编程技术网

Ruby on rails Rails 3关系

Ruby on rails Rails 3关系,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有两个模型,活动和城市 class activity has_many :attachments, :as => :attachable accepts_nested_attributes_for :attachments belongs_to city end class city has_many :activties end city_controller @activity_deals = @city.activities.find_all_by_dea

我有两个模型,活动和城市

class activity
  has_many :attachments, :as => :attachable 
  accepts_nested_attributes_for :attachments
  belongs_to city
end

class city
  has_many :activties
end

city_controller
  @activity_deals = @city.activities.find_all_by_deals(true)
end
观景城

 - @activity_deals.attachments.each do |a|
    = image_tag(a.file.url, :height =>"325px", :width =>"650px" )
       = a.description

我得到错误
未定义的方法
附件'`

您有类
附件
?您正在尝试调用附件类:
-@activity\u deals.attachments.each
。。。但是您得到的是
未定义的方法

因此,必须将此类添加到应用程序中:

class Attachment < ActiveRecord::Base
  belongs_to :activity
end
类附件
然而,我认为你正在尝试使用

如果是:

class Attachment < ActiveRecord::Base
  belongs_to :attachable, :polymorphic => true
end

class activity
  has_many :attachments, :as => :attachable 
  accepts_nested_attributes_for :attachments
  belongs_to city
end

class city
  has_many :activties
end
类附件true
结束
课堂活动
有多个:附件,:as=>:可附加
接受附件的\u嵌套\u属性\u
属于城市
结束
阶级城市
有很多活动吗
结束

您有class
附件吗?您正在尝试调用附件类:
-@activity\u deals.attachments.each
。。。但是您得到的是
未定义的方法

因此,必须将此类添加到应用程序中:

class Attachment < ActiveRecord::Base
  belongs_to :activity
end
类附件
然而,我认为你正在尝试使用

如果是:

class Attachment < ActiveRecord::Base
  belongs_to :attachable, :polymorphic => true
end

class activity
  has_many :attachments, :as => :attachable 
  accepts_nested_attributes_for :attachments
  belongs_to city
end

class city
  has_many :activties
end
类附件true
结束
课堂活动
有多个:附件,:as=>:可附加
接受附件的\u嵌套\u属性\u
属于城市
结束
阶级城市
有很多活动吗
结束

@activity\u deals
将是
activity
对象的
数组,而不是单个
activity
对象

我不是HAML用户,因此可能语法有误,但您可能可以使用类似以下内容:

 - @activity_deals.each do |activity|
    - activity.attachments.each do |a|
      = image_tag(a.file.url, :height =>"325px", :width =>"650px" )
         = a.description

确保查看整个错误消息,它将帮助您调试此类问题。整个消息类似于[…]的
未定义方法“attachments”:Array
,它告诉您正在
Array
上调用
attachments
,而不是
活动
活动
将是
活动
对象的
数组
,没有一个
活动
对象

我不是HAML用户,因此可能语法有误,但您可能可以使用类似以下内容:

 - @activity_deals.each do |activity|
    - activity.attachments.each do |a|
      = image_tag(a.file.url, :height =>"325px", :width =>"650px" )
         = a.description

确保查看整个错误消息,它将帮助您调试此类问题。整个消息类似于[…]:Array
未定义的方法“attachments”,它告诉您正在
数组上调用
附件,而不是
活动

。您似乎正在校准活动数组上的附件方法。。这就是为什么它会给你错误未定义的方法附件。请给出您的错误日志

您似乎正在校准一系列活动的附件方法。。这就是为什么它会给你错误未定义的方法附件。请在您的查看城市中提供错误日志,
@activity\u deals
是一个数组。因此,它没有定义“附件”方法

您必须访问数组中每个元素上的附件

就像这样:

- @activity_deals.attachments.each do |a|
= image_tag(a.file.url, :height =>"325px", :width =>"650px" )
   = a.description

- @activity_deals.each do |deal|
  - deal.attachments.each do |a|
    = image_tag(a.file.url, :height =>"325px", :width =>"650px" )
      = a.description

希望这有帮助

在您的view city中,
@activity\u deals
是一个数组。因此,它没有定义“附件”方法

您必须访问数组中每个元素上的附件

就像这样:

- @activity_deals.attachments.each do |a|
= image_tag(a.file.url, :height =>"325px", :width =>"650px" )
   = a.description

- @activity_deals.each do |deal|
  - deal.attachments.each do |a|
    = image_tag(a.file.url, :height =>"325px", :width =>"650px" )
      = a.description

希望这有帮助

它起作用了!谢谢HAML代码为城市x中的所有活动交易生成jquery内容滑块。我还有一个与活动具有相同关系的事件模型。我是否可以在一个查询中组合事件和活动,并将输出存储在@activity\u event\u deals中?非常感谢!它起作用了!谢谢HAML代码为城市x中的所有活动交易生成jquery内容滑块。我还有一个与活动具有相同关系的事件模型。我是否可以在一个查询中组合事件和活动,并将输出存储在@activity\u event\u deals中?非常感谢!