Из сообщений # show view комментарии могут быть добавлены только в том случае, если первый комментарий уже создан, в противном случае выдается эта ошибка маршрутизации:
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'
Если первый комментарий к посту уже создан
posts # show:
<%= 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