多态Avatar类在构建时堆栈级别太深

时间:2014-04-02 22:33:27

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 associations polymorphic-associations

我需要一些关于这个奇怪错误的帮助: 有2个班级:

class Avatar < ActiveRecord::Base

  belongs_to :avatarable, :polymorphic => true
  attr_accessible :photo_content_type, :photo_file_name, :photo_file_size, :photo

并且

class Group < ActiveRecord::Base
  include ActiveModel::Validations
  attr_accessible :location_attributes, :image_attributes, :name,
                   :location_id, :location_name, :scribble_attributes, 
                   :group_type, :privacy, :about, :membership_id, :event_id, :headline,   :avatar_attributes


  has_one :avatar, :as => :avatarable, :dependent => :destroy
  accepts_nested_attributes_for :avatar

这是一个show.html,如果它们存在,应该显示头像。

<% @groups.each do |g|%>
<h5>Name </h5>
<%=link_to g.name, group_path(g.id) %> <br>
<%if !g.avatar.nil?%> **also tried (g.avatar.photo.exists?)*(g.avatar.present?) (g.avatar.exists?)**
<li> <%= link_to image_tag(g.avatar.photo.url(:thumb)), group_path(g.id) %> </li> 
<%else%>
<li> <%= link_to image_tag(g.avatar.photo.url(:default_url)), group_path(g.id) %> </li> 
<%end%>
<%end%>

如果群组没有头像我会一直收到错误 - nil的未定义方法:NilClass

如果我在初始化或after_save上或在任何检查之前尝试做@ Group.build_avatar之类的操作 堆积级别深度错误

activerecord(3.2.13)lib / active_record / associations / association.rb:61

未定义的方法`照片'为nil:NilClass

app / views / groups / _groupsresults.html.erb:11:block in _app_views_groups__groupsresults_html_erb___226577206_56390436' app/views/groups/_groupsresults.html.erb:3:in每个' app / views / groups / _groupsresults.html.erb:3:in _app_views_groups__groupsresults_html_erb___226577206_56390436' app/views/groups/index.html.erb:14:in _ app_views_groups_index_html_erb___393629073_55205484' 请帮忙,这真是令人沮丧; /

0 个答案:

没有答案