0
votes

I have articles, profiles, and comments. There is a polymorphic association between articles/profiles and comments called commentable.

On success creating a new comment I return to the commentable parent object with a sucess flash and I would like to do the same with the appropriate error flash on validation errors.

What should I pass to render?

def create
  @commentable = find_commentable
  @comment = @commentable.comments.build(params[:comment])
  if @comment.save
    flash[:notice] = "Successfully created comment."
    redirect_to @commentable
  else
    render '??path_to_commentable_object_show??'
  end
end

I guess I could build the path by grabbing the commentable class name and lowercasing it... but that seems awkward.

1
Did you find a solution for this? I'm having the same issue and can't find out how to show the validation errors from the associated model. - Peter

1 Answers

0
votes

Building the path from the commentable class is generally what I would do.

In fact, you can build the path route helper name and then send that to the controller

path = "edit_"+commentable.class.to_s.dasherize.downcase+"_path
send(path.intern)