views:

161

answers:

1

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.

A: 

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)
Toby Hede
I need to load the commentable object and then render the show page for it... aka take them back to the display page for the article they tried to comment on, display the flash error message, and still have the original value they tried to enter.For the non polymorphic version I could do:@article = @comment.articlerender 'article/show'
SWR