这应该有点简单,但似乎无法掌握关联。
我正在使用nested_form 和回形针。我有一个名为 photo 的模型来存储所有图像和一个帖子模型。我正在尝试显示与相关帖子关联的照片,但在渲染视图时出现“未定义的方法头像”。
class Post < ActiveRecord::Base
has_many :photos, :dependent => :destroy
accepts_nested_attributes_for :photos
attr_accessible :title, :comments, :photo_id, :avatar, :photos_attributes
end
Class Photo < ActiveRecord::Base
belongs_to :post
attr_accessible :avatar, :post_id
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
控制器
def new
@post = Post.new
@post.photos.build
end
所以我的印象是,当建立一个帖子时,帖子和照片模型之间会建立关联吗?是对的吗?
所以当我在视图中调用它时,我得到了未定义的方法,谁能告诉我哪里出错了
<% @posts.each do |f| %>
<ul>
<li><%= f.title %></li>
<li><%= f.department.name %></li>
<li><%= image_tag f.avatar.url(:thumb) %></li>
<li><%= link_to "Delete Post", post_path(f.id), :confirm => "Are you sure?", :method => :delete %></li>
</ul>
<% end %>
我试过了
<%= image_tag f.photo.avatar.url(:thumb) %>
但这也不起作用