Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

Thomas Blumenfeld's Profile

GitHub User: thb

Site: tymate.com

Comments by Thomas Blumenfeld

Avatar

Hi, I would write the create method like this:

ruby
  def create
    @commentable = find_commentable
    instance_variable_set("@#{@commentable.class.to_s.downcase}", @commentable)
    comment = @commentable.comments.new(params[:comment])
    if comment.save
      redirect_to @commentable, :notice => "Comment successfully created!"
    else
      render "#{@commentable.class.to_s.tableize}/show"
    end
  end