为什么我无法获得/comments/new.html.erb?

问题描述:

我用脚手架命令创建的模型为什么我无法获得/comments/new.html.erb?

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)%> 

在这里,我想我需要......(@后,@comment),就像从http://guides.rubyonrails.org/routing.html

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

,但我还没有@comment这里。

我得到错误:

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> 

需要怎么我写这样才能得到/comments/new.html.erb

今天我已经纠正了我的代码: /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 

我再次得到了同样的错误:

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' 

问题在哪里?我该如何解决它?

+1

''尝试删除空间:'' –

+0

空间不应该这个行的结果是一样的:'(@post)'变成'@ post'并传递给'new_post_comment_path',结果成为'link_to'的第二个参数 – Leito

它,因为你不必只是'/comments/new'路径,但form_for(@comment)试图创建它。

您的路线正在创建类似'/post/:id/comments/new'的路径,因此您必须使用form_for([@post, @comment])

同样在新方法中添加@post = Post.find(params(:is))或者更好地调用before_action回调。

+0

我已经在帖子#新: – Vladimir

+0

我已经完成了写在帖子#新:我得到了同样的错误@ post = Post.find(params [post_id])@comment [email protected] – Vladimir

+0

1.你想要使用的路线是:'posts /:post_id/comments:/new',所以你的链接应该是'link_to'Add Comment',new_post_comment_path(@post)'。'commentscontroller#new'你必须找到@post,然后建立新的@comment。@ post.comments.new或@ post.comments.build。你的form_for应该看起来像'form_for([@ post,@comment])'。 – Argonus

你得到,因为错误说的错误,是对_comments/form.html.erb

form_for(@comment) 

您需要@post对象在那里,form_for推测正确的路径:

form_for([@post, @comment]) 
+0

Where我可以评论吗? – Vladimir

+0

对不起,我的意思是@comment,如果没有评论,你仍然想要显示表单,你可以通过'@comment = @ post.comment.build'为你的表单建立一个空的评论来显示。 – Leito

+0

我曾试过我得到了错误。我明天写下这个错误。我忘了提交工作电脑 – Vladimir