嵌套资源重定向到新的

时间:2015-06-25 00:44:43

标签: ruby-on-rails

在Ryan的Railscast#154中,他讨论了多态关联。 http://railscasts.com/episodes/154-polymorphic-association-revised

此处新注释表单显示为父项目的show动作的一部分,例如文章。

def show
  @article = Article.find(params[:id])
  @commentable = @article
  @comments = @commentable.comments
  @comment = Comment.new
end

提交表单时,评论=>新行动处理请求。

def create
  @comment = @commentable.comments.new(params[:comment])
  if @comment.save
    redirect_to @commentable, notice: "Comment created."
  else
    render :new
  end
end

但是当注释记录无法保存到注释控制器的控制器“新”视图时,这很有效。

else
  render :new

这会将用户带到评论新表单。对于控制器呈现父控制器,文章或其他方式的“显示”动作会更好。

我很想知道如何从@commentable项呈现视图。我试过以下但没有成功。

else
  render [:new, @commentable]

0 个答案:

没有答案