开发者

Rails polymorphic assosication - on validation error render parent show action with flash message

开发者 https://www.devze.com 2022-12-17 23:55 出处:网络
I have articles, pr开发者_Python百科ofiles, and comments. There is a polymorphic association between articles/profiles and comments called commentable.

I have articles, pr开发者_Python百科ofiles, 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.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号