我正在使用有关Rails入门的教程http://tutorials.jumpstartlab.com/projects/blogger.html 我已经完成了该教程,现在我正在使用创建的学校作业站点,我只想在db / migrate / 20161005160810_create_arti ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我是RoR的新手,我正尝试按照http://guides.rubyonrails.org/getting_started.html上的指南进行操作。我目前位于第6节第4点,但我一直被困在这里。
我收到以下错误:Articles#show中的NoMethodError
Showing /Users/riaanvanstraaten/rubyapps/blog/app/views/articles/show.html.erb where line #12 raised:
undefined method `article_comments_path' for #<#<Class:0x007fac016d6d20>:0x007fac01c05f30>
Extracted source (around line #12):
10
11 <h2>Add a comment:</h2>
12 <%= form_for([@article, @article.comments.build]) do |f| %>
13 <p>
14 <%= f.label :commenter %><br>
15 <%= f.text_field :commenter %>
我已经仔细检查了指南中的代码,但是看不到哪里出了问题。
我一直在寻找一些建议,并遵循了一些建议,但都没有取得积极的结果。 我有一篇帖子非常贴近我的问题,但是上述解决方案无法解决我当前的问题。 这是指向类似文章的链接: Articles#show.undefined方法“ article_comments_path”中的NoMethodError
这是使用的文件中的代码
show.html.erb
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Text:</strong>
<%= @article.text %>
</p>
<h2>Add a comment:</h2>
<%= form_for([@article, @article.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %> |
<%= link_to 'Edit', edit_article_path(@article) %>
comments_controller.html.erb
class CommentsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
的routes.rb
Rails.application.routes.draw do
resources :articles do
resources :commments
end
end
这是我的“耙路”输出
Prefix Verb URI Pattern Controller#Action
article_commments GET /articles/:article_id/commments(.:format) commments#index
POST /articles/:article_id/commments(.:format) commments#create
new_article_commment GET /articles/:article_id/commments/new(.:format) commments#new
edit_article_commment GET /articles/:article_id/commments/:id/edit(.:format) commments#edit
article_commment GET /articles/:article_id/commments/:id(.:format) commments#show
PATCH /articles/:article_id/commments/:id(.:format) commments#update
PUT /articles/:article_id/commments/:id(.:format) commments#update
DELETE /articles/:article_id/commments/:id(.:format) commments#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
怎么了? 我从网站复制并粘贴了代码,以查看我做错了什么,但仍然无法解决问题。
在此先感谢您的帮助!
您的routes.rb
有一个错字。 您的单词注释中有3 m
字母,而不是2个:
Rails.application.routes.draw do
resources :articles do
resources :commments
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.