2012-07-29 2 views
0

I가 2 개 모델, 활동 및 도시레일 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_deals(true) 
end 

보기 도시 내가 할

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

오류 undefined method 첨부 파일 '`

답변

1

@activity_dealsArrayActivity의 객체가 아닌 하나의 Activity 객체가 될 것입니다.

은 내가 HAML 사용자가 아닌, 그래서 나는 구문이 잘못 받고있을 수도 있지만, 당신은 아마 이런 비트 뭔가를 사용할 수 있습니다

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

하면 전체 오류 메시지가보고 있는지 확인하십시오, 그것은 이런 종류의 문제를 디버그하는 데 도움이 될 것입니다. 전체 메시지는 undefined method 'attachments' for […]:Array과 같을 것이며 attachmentsArray이고 Activity이 아니라고 알려줍니다.

+0

작동합니다! 고맙습니다 .. HAML 코드는 도시 x의 모든 활동 거래에 대한 jquery 콘텐츠 슬라이더를 생성합니다. 나는 또한 활동과 같은 관계를 가진 이벤트 모델을 가지고있다. 하나의 쿼리에서 이벤트와 활동을 결합하고 @activity_event_deals에 출력을 저장할 수 있습니까? 감사합니다. – Remco

+0

당신은 라이트입니다 ... 나는 이미 해결책을 찾았습니다. – Remco

1

당신이 클래스 Attachement이 있습니까? 당신이 첨부 파일 클래스를 호출하려고 : - @activity_deals.attachments.each ...하지만 당신은 그래서 undefined method ...
을 받고, 당신이 응용 프로그램에이 클래스를 추가해야합니다

class Attachment < ActiveRecord::Base 
    belongs_to :activity 
end 

을하지만, 나는 당신이하려고하는 생각 polymorphic을 사용하십시오.

그렇다면 :

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 
0

당신은 활동의 배열에 첨부 파일 메서드를 caling하는 것 같습니다. 그게 왜 당신에게 오류 정의되지 않은 ethod attacments주는. u 오류 로그를 보내주십시오.

1

시에서 @activity_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 

희망!