0

I created models by scaffold command

class Post < ActiveRecord::Base
  has_many :comments, as: :commentable
end

class Comment < ActiveRecord::Base
  belongs_to :commentable , polymorphic: true
end

...

routes.rb:

 resources :posts, :images, :links do
    resources :comments
 end

comments_controller.rb:

def new
  @comment = Comments.new
end

/posts/show.html.erb:

 <%= link_to 'Add comment', new_post_comment_path (@post)%>

Here I think I need ...(@post, @comment),like from http://guides.rubyonrails.org/routing.html:

<%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %>

but I haven't @comment here.

I get error:

    Showing /home/loza/Projects/my_blog/app/views/comments/_form.html.erb where line #1 raised:

    undefined method `comments_path' for #<#<Class:0x007f2e4a77c2f0>:0x007f2e4ab23ef8>

Extracted source (around line #1): 
<%= form_for(@comment) do |f| %>
  <% if @comment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

      <ul>

How need I write so as to get /comments/new.html.erb?

Today I've corrected my code: /posts/show.html.erb:

<%= link_to 'New comment', new_post_comment_path(@post, @post.comments.build) %>

/comments_controller.rb:

  def new
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new
  end

I got the same error again:

Showing /home/loza/Projects/my_blog/app/views/comments/_form.html.erb where line #1 raised:

undefined method `comments_path' for #<#<Class:0x007fa736859320>:0x007fa73669e238>

xtracted source (around line #1):

  <%= form_for(@comment) do |f| %>
  <% if @comment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

app/views/comments/_form.html.erb:1:in `_app_views_comments__form_html_erb___1254360398011104975_42800220'
app/views/comments/new.html.erb:3:in `_app_views_comments_new_html_erb__2117553728149416519_42948680'

Where is problem ? How can I solve it?

Vladimir
  • 67
  • 5
  • 1
    `<%= link_to 'Add comment', new_post_comment_path (@post)%>` try to remove space: `<%= link_to 'Add comment', new_post_comment_path(@post)%>` – Alex Zakruzhetskyi Oct 03 '16 at 14:58
  • The space shouldn't matter, it's nice to have consistent spacing, but the result for this line is the same: `(@post)` becomes `@post` and that's passed to `new_post_comment_path`, the result becomes the second argument for `link_to` – Leonel Galán Oct 03 '16 at 15:08

2 Answers2

1

The error you are getting, as the error says, is on _comments/form.html.erb:

form_for(@comment)

You need the @post object in there, for form_for to figure the right path:

form_for([@post, @comment])
Leonel Galán
  • 6,993
  • 2
  • 41
  • 60
  • Where can I take comment? – Vladimir Oct 03 '16 at 17:43
  • I'm sorry I meant @comment, if there are no comments and you still want to show the form you could do `@comment = @post.comment.build` to build an empty comment for your form to display. – Leonel Galán Oct 03 '16 at 18:51
  • I had tried <%= link_to 'Add comment', new_post_comment_path ([@post, @post.comments.build )%> I got error. I cat write this error tomorrow. I forgot commit at work computer – Vladimir Oct 03 '16 at 19:00
  • The add comment link was OK, the error was on your actual form (see my answer: the error is on __comments/form.html.erb_. – Leonel Galán Oct 03 '16 at 19:24
0

Its because you don't have just '/comments/new' path but form_for(@comment) is trying to create it.

Your routes are creating path like '/post/:id/comments/new' so you have to use form_for([@post, @comment]).

Also add @post = Post.find(params(:is)) in new method or better as before_action callback.

Argonus
  • 1,005
  • 6
  • 11
  • I had written in posts#new: I got the same error @post=Post.find(params[post_id]) @comment =@post.comments.new – Vladimir Oct 03 '16 at 19:05
  • 1. route you want to use is: `posts/:post_id/comments:/new`, so your link should be `link_to 'Add Comment', new_post_comment_path(@post)`. In `commentscontroller#new` you have to find @post, and build new @comment. @post.comments.new or @post.comments.build. you form_for should look like `form_for([@post, @comment])`. – Argonus Oct 04 '16 at 08:30
  • I've corrected form_for and comments/new and it works. Thanks. – Vladimir Oct 04 '16 at 08:37