Reputation: 765
From the posts#show view comments can only be added if the first comment has already been created, otherwise it throws this routing error:
No route matches {:action=>"show", :controller=>"posts", :view=>"comments"}
Terminal reads:
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'
If the first comment for a post is already created
posts#show:
<%= render @post %>
<%= nested_comments @comments.arrange(:order => :created_at) %>
<%= render "comments/form" %>
the 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 %>
comments controller:
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
routes:
root :to => "posts#index"
resources :topics
resources :posts
resources :comments
schema:
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
Upvotes: 0
Views: 995
Reputation: 7072
It looks like your form isn't being tied to an actual comment since it's just being passed a symbol. Try this is your view
<%= simple_form_for @comment, :url => { :controller => :comments, :action => "create" } do |f| %>
...
Actually that should allow you to ditch the :url
parameter as well
<%= simple_form_for @comment do |f| %>
Then just makes sure you define @comment
in your controller and set the post_id
@comment = Comment.new
@comment.post_id = @post.id
Upvotes: 0
Reputation: 56729
I haven't tested this but try replacing
redirect_to(post_path(@comment.post, :view => "comments"))
in your controller's create action with:
redirect_to(post_path(:id => @comment.post_id, :view => "comments"))
If the above doesn't work, we'll try something else. That seems to be the location of your problem, in any case.
Upvotes: 1