从岗位,如果第一个评论已创建#秀查看评论只能加到路由错误,否则就抛出此路由错误:接收在提交评论表单
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
'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
@BasicObject - 你没有'post_id',这甚至没有被传递到创建操作中。当你进入'new'动作时,是否有可能在没有任何params [:post_id]的情况下运行此测试?我建议您在尝试保存并重定向之前确保您已在@comment上设置了post_id,否则,如果这些post_id参数不存在,您将在未来留下自己的错误。 – sscirrus 2011-05-18 07:23:07
因此,在我的控制器的创建操作中,我应该在新操作中使用'@ comment'类似的东西吗? '@comment = Comment.create(:parent_id => params [:parent_id],:post_id => params [:post_id])'?我还没回家去试试。 – BasicObject 2011-05-19 02:06:44