为什么我不能得到 /comments/new.html.erb?
Why can't I get /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)%>
这里我想我需要...(@post, @comment),like from 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'
问题出在哪里?我该如何解决?
如错误所述,您遇到的错误位于 _comments/form.html.erb:
form_for(@comment)
您需要 @post 对象,以便 form_for
找到正确的路径:
form_for([@post, @comment])
这是因为您不仅有 '/comments/new'
路径,而且 form_for(@comment)
正在尝试创建它。
您的路线正在创建类似于 '/post/:id/comments/new'
的路径,因此您必须使用 form_for([@post, @comment])
。
同时在新方法中添加 @post = Post.find(params(:is))
或更好的 before_action 回调。
我通过脚手架命令创建了模型
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)%>
这里我想我需要...(@post, @comment),like from 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'
问题出在哪里?我该如何解决?
如错误所述,您遇到的错误位于 _comments/form.html.erb:
form_for(@comment)
您需要 @post 对象,以便 form_for
找到正确的路径:
form_for([@post, @comment])
这是因为您不仅有 '/comments/new'
路径,而且 form_for(@comment)
正在尝试创建它。
您的路线正在创建类似于 '/post/:id/comments/new'
的路径,因此您必须使用 form_for([@post, @comment])
。
同时在新方法中添加 @post = Post.find(params(:is))
或更好的 before_action 回调。