I have a Post model, which has many Post_Images (polymorphic as :imageable). I am trying to make a nested form where Posts can be created, including Post_Images, all while simultaneously assigning the post to groups (there is a group model which has_many posts :through :assignments, hence the form_for [@user, @post]
below).
Models:
Post Model
class Post < ActiveRecord::Base
# attr_accessible :name, :borrow, :price, :description, :user_id, :product_category_id, :post_image_ids, :group_ids, :post_images_attributes, :post_image
has_many :post_images, :as => :imageable, :dependent => :destroy
accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true
.
.
.
end
PostImage model (should be just Image, but I made it ahile ago and can't refactor it):
class PostImage < ActiveRecord::Base
# attr_accessible :imageable_id, :imageable_type, :post_image_ids
belongs_to :imageable, :polymorphic => true
has_attached_file :image, :styles => { :small => "150x150>", :large => "320x240>" }
validates_attachment_presence :image
validates_attachment_size :image, :less_than => 5.megabytes
end
PostsController
def new
@user = User.find(params[:user_id])
@post = @user.posts.build
@assigment = Assignment.new
#allow up to 5 images to be uploaded
#5.times {@post.build_post_image}
post_image = @post.post_images.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
end
end
def create
@user = User.find(params[:user_id])
params[:post][:group_ids] ||= []
@post = @user.posts.build(params[:post])
if @post.save
redirect_to @user
else
redirect_to new_user_post_path(current_user)
end
end
The blasted form: (builds posts, while also adding them to groups)
=nested_form_for( [@user, @post], :remote => true) do |f|
=hidden_field_tag 'user_id', @user.id
=f.fields_for :post_images
=f.link_to_add "Add an image", :post_images
=f.label :name
=f.text_field :name
=f.label :borrow, "borrow?"
=f.check_box :borrow
=f.label :price
=f.text_field :price
=f.label :description
=f.text_area :description
%h2 choose which groups you want to post this item to
-@user.groups_as_owner.each do |group|
= label_tag "group_checkbox_#{group.id}", group.name
= check_box_tag("post[group_ids]", group.id, @user.groups_as_owner.include?(group), :id => "group_checkbox_#{group.id}")
-@user.groups_as_member.each do |group|
= label_tag "group_checkbox_#{group.id}", group.name
= check_box_tag("post[group_ids]", group.id, @user.groups_as_member.include?(group), :id => "group_checkbox_#{group.id}")
.action=f.submit "Save Post"
_post_image_fields partial
=f.label :post_image, "Image"
%br
=f.file_field :post_image
=f.link_to_remove "remove"
I have attr_accessible commented out because I was getting a WARNING: Can't mass-assign protected attributes
and in searching for a resolutio to this, I happened on a bunch of resources including:
nested form with polymorphic models
google group convo addressing mass-assignment warning
To my knowledge, none of them really addressed my issue. Maybe it is because of the extra aspect I have in my form- the creation of group assignments? It seems to me though that it is a param issue. Because I got it to the point where the form would work...with a lot of attr_accessible in place, but it would say that it couldn't mass-assign to :post_image
which I made accessible in PostImages and Posts...so I wasn't sure what the issue with that was.
lastly, is the answer in this post?(i don't quite understand it): ActiveRecord, has_many :through, and Polymorphic Associations
Let me know if I should supply more info, somehow make this clearer. Happy Holidays all!