通过link_to传递参数时没有Route错误

发布于 2024-11-06 04:29:38 字数 1992 浏览 5 评论 0原文

错误:

没有路由匹配 {:action=>"new", :controller=>"comments", :parent_id=>1}

paths.rb:

MyApp::Application.routes.draw do
  resources :posts do
    resources :comments
  end
  resources :topics do
    resources :posts
  end
  root :to => "posts#index"
end

models:

class Topic < ActiveRecord::Base
  has_many        :posts, :dependent => :destroy
  attr_accessible :name, :post_id
end

class Post < ActiveRecord::Base
  belongs_to :topic,    :touch => true
  has_many   :comments, :dependent => :destroy
  accepts_nested_attributes_for :topic
  attr_accessible :name, :title, :content, :topic, :topic_attributes
end

class Comment < ActiveRecord::Base
  has_ancestry
  attr_accessible :name, :content
  belongs_to      :post, :touch => true
end

view:

<%= link_to "Reply", new_post_comment_path(@post, :parent_id => comment.id) %> 

controller:

class CommentsController < ApplicationController
  respond_to :html, :xml
  def show
    @post = Post.find(params[:id])
    @comments = @post.comments.order("updated_at").page(params[:page])
  end

  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.build(params[:comment])
    if @comment.save
      flash[:notice] = "Replied to \"#{@post.title}\""
      redirect_to(@post)
    else
      flash[:notice] = "Reply failed to save."
      redirect_to(@post)
    end
  end

  def new
    @post = Post.find(params[:post_id])
    @comment = Comment.new(:parent_id => params[:parent_id]) 
    # @comment = @post.comments.build
  end

  def destroy
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    @comment.destroy
    redirect_to post_path(@post)
  end
end

通过阅读您可能已经收集到我正在尝试让 ancestry gem 与嵌套资源一起使用的代码。我一直在使用关于 Ancestry gem 的 Railscasts 剧集来指导我。感谢您阅读我的问题。

Error:

No route matches {:action=>"new", :controller=>"comments", :parent_id=>1}

routes.rb:

MyApp::Application.routes.draw do
  resources :posts do
    resources :comments
  end
  resources :topics do
    resources :posts
  end
  root :to => "posts#index"
end

models:

class Topic < ActiveRecord::Base
  has_many        :posts, :dependent => :destroy
  attr_accessible :name, :post_id
end

class Post < ActiveRecord::Base
  belongs_to :topic,    :touch => true
  has_many   :comments, :dependent => :destroy
  accepts_nested_attributes_for :topic
  attr_accessible :name, :title, :content, :topic, :topic_attributes
end

class Comment < ActiveRecord::Base
  has_ancestry
  attr_accessible :name, :content
  belongs_to      :post, :touch => true
end

view:

<%= link_to "Reply", new_post_comment_path(@post, :parent_id => comment.id) %> 

controller:

class CommentsController < ApplicationController
  respond_to :html, :xml
  def show
    @post = Post.find(params[:id])
    @comments = @post.comments.order("updated_at").page(params[:page])
  end

  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.build(params[:comment])
    if @comment.save
      flash[:notice] = "Replied to \"#{@post.title}\""
      redirect_to(@post)
    else
      flash[:notice] = "Reply failed to save."
      redirect_to(@post)
    end
  end

  def new
    @post = Post.find(params[:post_id])
    @comment = Comment.new(:parent_id => params[:parent_id]) 
    # @comment = @post.comments.build
  end

  def destroy
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    @comment.destroy
    redirect_to post_path(@post)
  end
end

By reading the code you might have gathered that I am trying to get the ancestry gem to work with nested resources. I've been using the Railscasts episode on the Ancestry gem to guide me. Thanks for reading my question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世蒼涼 2024-11-13 04:29:38

尝试传递评论id

link_to "Reply", new_post_comment_path(@post, :parent_id => comment.id).

Try to pass comment id

link_to "Reply", new_post_comment_path(@post, :parent_id => comment.id).
时光倒影 2024-11-13 04:29:38

您需要使用嵌套路径:link_to "Reply", new_post_comment_path(@post, :parent_id => comment)

rake paths 可以成为你的朋友。

You need to use the nested path: link_to "Reply", new_post_comment_path(@post, :parent_id => comment).

rake routes can be your friend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文