通过link_to传递参数时没有Route错误
错误:
没有路由匹配 {: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试传递评论id
Try to pass comment id
您需要使用嵌套路径:
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.