A user can create different kinds of posts. I setup a polymorphic relation.
Post
class Post < ActiveRecord::Base
attr_accessible :user_id, :address
belongs_to :postable, polymorphic: true, dependent: :destroy
belongs_to :user
validates_presence_of :user_id, :postable_id, :postable_type
end
NeighborhoodPost
class NeighborhoodPost < ActiveRecord::Base
has_one :user, through: :post
has_one :post, as: :postable, autosave: true, validate: false
attr_accessible :content, :title, :post_attributes
accepts_nested_attributes_for :post
end
NeighborhoodPostsController
def create
params[:neighborhood_post][:post_attributes][:user_id] = current_user.id
@neighborhood_post = NeighborhoodPost.new(params[:neighborhood_post])
if @neighborhood_post.save
redirect_to root_url, notice: 'NeighborhoodPost was successfully created.'
else
render action: "new"
end
end
Neighborhood post form
= f.fields_for :post do |post_builder|
.control-group
= post_builder.label :address, 'Adres', class: 'control-label'
.controls
= post_builder.text_field :address, placeholder: "Adres voor locatie"
This actually works. However, I don't like editing params in the create action. When I try to do the following:
@neighborhood_post = current_user.neighborhood_posts.create(params[:neighborhood_post])
...it actually creates two posts. One with a user_id set and where address is nil one where user_id is nil and address is filled with data. How come!