This should be somewhat simple but cant seem to grasp the association.
I am using nested_form and paperclip. I have a model called photo to store all images and a post model. I am trying to show the photos associated to the relevant post but am getting ‘undefined method avatar’ when rendering the view.
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
Controller
def new
@post = Post.new
@post.photos.build
end
So i am under the impression that when a post gets built an association between the Post and Photo model is made? is that right?
So when i call this in the view i get the undefined method, can anyone advise where I am going wrong please
<% @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 %>
I have tried
<%= image_tag f.photo.avatar.url(:thumb) %>
but that doesnt work either
May be you are creating photo wrong.
Here you can see how the form looks: Nested form using paperclip
And also
Posthas_many :photos, so it must be somth. like