On Oct 14, 4:40 am, Serafino P. <rails-mailing-l…@andreas-
I have basically posts and articles models which relate to the
polymorphic model comment.
Routes are generated as I expect to, but the problem resides in the
comments_controller, since in this controller I’m not able to figure out
if I’m creating a comment for a post or for an article.
Is there a (possibly clean) way to solve this?
You can check for the presence of params[:post_id] or
params[:article_id] to discover the route that got triggered.
I generally use a before_filter:
class CommentsController < ApplicationController
@parent = Article.find_by_id(params[:article_id]) ||
return unless params[:id]
raise ActiveRecord::RecordNotFound unless
This will first look for an article, and if that fails (returns nil),
it will assume it’s a post. Raises an exception of neither are
found. The second filter helps ensure that someone didn’t mess with
Does this help?