使用 polymorphic_path 函数时,出现“undefined method 'merge'”错误。

4

我正在使用多态性(polymorphism)为我的文章(Article)、个人资料(Profile)和照片(Photo)模型创建评论。这是我的文章显示页面的视图(view)代码:

<div id="comment <%= comment.id %>">
  <%= comment.title %>
  | <%= link_to "Permalink", polymorphic_path(@commentable, comment), :action => :show %>
  | <%= link_to "Reply", polymorphic_path(comment, @comment_child), :action => :new %>
  | <%= link_to "Edit Comment", polymorphic_path(@commentable, comment), :action => :edit %>
  | <%= link_to 'Delete Comment', [@commentable, comment], :confirm => "Are you sure?", :method => :delete %><br />
  <%= comment.content %><br />
  <%= comment.user.name %><br /><br />
  <%= render :partial => 'comments/comment', :collection => @comment.children %>
</div>

以下是“show”控制器的文章:

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

错误出现在这一行:
   | <%= link_to "Permalink", polymorphic_path(@commentable, comment), :action => :show %>

但我确信这个错误存在于所有使用polymorphic_path的其他行中。如果我将polymorphic_path替换为article_comment_path,则可以正常工作。

非常感谢您的帮助。

1个回答

12

根据多态路径的定义,它期望一个参数,然后是一个可选的哈希。我认为,在这种情况下,您需要将@commentable和comment作为一个数组中的一个参数传递。

polymorphic_path([@commentable, comment])


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接