When a user creates a post I also want to save all the tags
associated with it. I do that in the post_controller like this
presently:
def save
@post = Post.new(@params[:post])
@tags = Tag.new(@params[:tags])
@post.user_id = session[:id]
if @post.save
@tags.title.split(',').each do |tag|
present_tag = Tag.find_by_title(tag.strip)
present_tag = Tag.create(:title => tag.strip) if present_tag.nil?
@post.tags.push(present_tag)
end
flash[:notice] = "Successfully Created Post!"
redirect_to :controller => "home", :action => "index"
else
render :action => "new"
end
end
Now I have a couple questions:
- Is there an easier way to save both the tags and the post at the
same time? - Do I need to do the push to save the join table or is there a more
elegant way or automated way of doing this? - Does the fact that I have to check to see if tag already exist
change things?
Your feedback is greatly appreciated. I find my solution not that
elegant and would love to find a new one. This is for my site http://
www.soen.info.
Your Friend,
John K.
–
http://www.soen.info - source of the freshest software engineering
information on the net
http://cusec.soen.info - software engineering conference
http://www.kopanas.com - personal site