2011-05-18 87 views
0

从岗位,如果第一个评论已创建#秀查看评论只能加到路由错误,否则就抛出此路由错误:接收在提交评论表单

No route matches {:action=>"show", :controller=>"posts", :view=>"comments"} 

终端读取:

Started POST "/comments" for 127.0.0.1 at 2011-05-17 18:18:03 -0700 
    Processing by CommentsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"BPeNvVJh+dLBhpiZK16phQHp7UV4MasPyQW6PL9VG8I=", "comment"=>{"post_id"=>"", "parent_id"=>"", "name"=>"", "content"=>"yo"}, "commit"=>"Reply"} 
AREL (0.9ms) INSERT INTO "comments" ("name", "content", "post_id", "created_at", "updated_at", "ancestry") VALUES ('', 'yo', NULL, '2011-05-18 01:18:03.445466', '2011-05-18 01:18:03.445466', NULL) 
Completed in 133ms 

ActionController::RoutingError (No route matches {:action=>"show", :controller=>"posts", :view=>"comments"}): 
    app/controllers/comments_controller.rb:14:in `block (2 levels) in create' 
    app/controllers/comments_controller.rb:9:in `create' 

如果已经创建了一个帖子第一条评论

帖子#显示:

<%= render @post %> 

<%= nested_comments @comments.arrange(:order => :created_at) %> 

<%= render "comments/form" %> 

形式:

<%= simple_form_for :comment, :url => { :controller => :comments, :action => "create" } do |f| %> 
    <%= f.input :post_id, :required => false, :as => :hidden %> 
    <%= f.input :parent_id, :required => false, :as => :hidden %> 
    <%= f.input :name, :label => false, :placeholder => "Name (optional)", :required => false %> 
    <%= f.input :content, :label => false, :placeholder => "Reply", :as => :text %> 
    <%= f.button :submit, "Reply" %> 
<% end %> 

评论控制器:

def new 
    @comment = Comment.new(:parent_id => params[:parent_id], :post_id => params[:post_id]) 
end 

def create 
    @comment = Comment.create(params[:comment]) 
    @comment.save 
    respond_to do |format| 
    format.html do 
     if @comment.errors.present? 
     render :new 
     else 
     redirect_to(post_path(@comment.post, :view => "comments")) 
     end 
    end 
    format.js 
    end 
end 

路线:

root :to => "posts#index" 
resources :topics 
resources :posts 
resources :comments 

架构:

create_table "comments", :force => true do |t| 
    t.string "name" 
    t.text  "content" 
    t.integer "post_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "ancestry" 
end 

add_index "comments", ["ancestry"], :name => "index_comments_on_ancestry" 

create_table "posts", :force => true do |t| 
    t.string "name" 
    t.string "title" 
    t.text  "content" 
    t.integer "topic_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

create_table "topics", :force => true do |t| 
    t.string "name" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

回答

1

我没有测试过这一点,但尝试更换

redirect_to(post_path(@comment.post, :view => "comments")) 

在你的控制器的创建操作:

redirect_to(post_path(:id => @comment.post_id, :view => "comments")) 

如果上述方法无效,我们将别的尝试一些。无论如何,这似乎是你问题的地点。

+0

'ActionController :: RoutingError(No route matches {:action =>“show”,:controller =>“posts”,:id => nil,:view =>“comments”}): app/controllers/comments_controller.rb:14:在'block(2 levels)in' app/controllers/comments_controller.rb:9:in'create'' – BasicObject 2011-05-18 05:17:37

+0

@BasicObject - 你没有'post_id',这甚至没有被传递到创建操作中。当你进入'new'动作时,是否有可能在没有任何params [:post_id]的情况下运行此测试?我建议您在尝试保存并重定向之前确保您已在@comment上设置了post_id,否则,如果这些post_id参数不存在,您将在未来留下自己的错误。 – sscirrus 2011-05-18 07:23:07

+0

因此,在我的控制器的创建操作中,我应该在新操作中使用'@ comment'类似的东西吗? '@comment = Comment.create(:parent_id => params [:parent_id],:post_id => params [:post_id])'?我还没回家去试试。 – BasicObject 2011-05-19 02:06:44

0

看起来你的表单并没有被绑定到实际的评论,因为它只是被传递了一个符号。试试这是你的看法

<%= simple_form_for @comment, :url => { :controller => :comments, :action => "create" } do |f| %> 
... 

其实这应该让你沟:url参数以及

<%= simple_form_for @comment do |f| %> 

然后就可以确保你在你的控制器定义@comment并设置post_id

@comment = Comment.new 
@comment.post_id = @post.id 
+0

这并不直接解决OP的问题。这个错误,特别是终端读出,表明数据被成功提交到'create'动作 - 它在保存发生后重定向_after_时遇到了麻烦。 – sscirrus 2011-05-18 04:37:34

+0

它看起来像'post_id'和'parent_id'没有通过参数传递:'“comment”=> {“post_id”=>“”,“parent_id”=>“”,“name”= >“”,“content”=>“yo”}'这应该是为什么它不能重定向到帖子。 – aNoble 2011-05-18 04:52:43

+0

你是对的...可能OP在他/她进入新行动时没有'params [:post_id]'? – sscirrus 2011-05-18 07:24:04