I have two models, Posts and Hashtags, in a HABTM relationship.
My post controller looks like this:
def create
@post = current_user.posts.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, :notice => 'Post was successfully created.' }
format.json { render :json => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.json { render :json => @post.errors, :status => :unprocessable_entity }
end
end
end
Now, when a user creates a new post, I want to do the following:
- check for hashtags, words starting with a '#'
- if the hashtag already exists, link the Post and the Hashtag
- if the hashtag does not exist, add it to Hashtags, and link the Post and the newly added Hashtag
I suppose I have to do this after I do the posts.new, but I am unsure how to proceed. Do I just edit the @post object? If so, what format should it be in for @post.save to pick up on the link?
Any suggestions would be greatly appreciated!