ruby on rails - No Route error when passing parameters through link_to -
error:
no route matches {:action=>"new", :controller=>"comments", :parent_id=>1}
routes.rb:
myapp::application.routes.draw resources :posts resources :comments end resources :topics 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 \"#{@post.title}\"" redirect_to(@post) else flash[:notice] = "reply failed 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 code might have gathered trying ancestry
gem work nested resources. i've been using railscasts episode on ancestry gem guide me. reading question.
try pass comment id
link_to "reply", new_post_comment_path(@post, :parent_id => comment.id).
Comments
Post a Comment