我在做http://guides.rubyonrails.org/getting_started.html教程,使用Rails 5&创建博客Ruby 2.4。在复制并粘贴到第6单元:添加注释模型的末尾之后,Rails抛出了这个错误:
“文章中的NoMethodError#Show”:未定义的方法 #article的`article_comments_path'#
<h2>Add a comment:</h2>
<%= form_for([@article, @article.comments.build]) do |f| %><!--****Error?****-->
2014年10月26日的Stackoverflow回答说要将routes_comments_path帮助方法添加到routes.rb,如下所示:
resources :articles do
resources :comments
end
但是语法似乎有所改变。
我的routes.rb看起来像这样:
Rails.application.routes.draw do
resources :articles
resources :comments#This creates comments as a nested resource within articles.
root 'welcome#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
我没有在终端中输入任何拼写错误,所以我不确定如何继续。非常感谢任何指导。
我的'佣金路线| grep评论的输出是:
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#create
new_comment GET /comments/new(.:format)评论#new edit_comment获取/comments/:id/edit(.:format)评论#edit 评论GET /comments/:id(.:format)评论#show PATCH /comments/:id(.:format)评论#news PUT /comments/:id(.:format)评论#news DELETE /comments/:id(.:format)comments#destroy
和我的rake routes
输出为:
Prefix Verb URI Pattern Controller#Action
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
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#create
new_comment GET /comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PATCH /comments/:id(.:format) comments#update
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
root GET / welcome#index
有什么问题吗?
答案 0 :(得分:0)
根据您提供的文档,您应该使用块来嵌套路由:
Rails.application.routes.draw do
resources :articles
resources :comments#This creates comments as a nested resource within articles.
root 'welcome#index
end
Ruby不知道这个缩进,所以您的代码格式如下:
rake routes
编辑:您始终可以使用 <div class="comment_list" v-for="comment in all_comments">
<a href="#" class="initial" v-on:click.prevent="replyBox(comment)">REPLY</a>
<div id="reply-box-@{{comment.id}}" class="reply-box" v-bind:class="{active: comment.isActive}">
<div class="user_comment row">
<div class="col-md-1 col-sm-1 col-xs-1">
<div class="user_profile_image {{ isset($current_user->personal_user) ? 'bg_blue' : 'bg_green'}}">
@if(isset($current_user->avatar) && $current_user->avatar != '')
<img src="{{ avatar_path($current_user->avatar)}}" alt="" />
@else
<img src="{{ home_asset('img/user_icon.png') }}" alt="" />
@endif
</div>
</div>
<div class="col-md-11 col-sm-11 col-xs-11">
<textarea class="comment_input" placeholder="Join the discussion..." @keydown.enter.prevent="postComment({{$current_user->id}}, {{$article->id}})" v-model.trim="reply_comment"></textarea>
</div>
</div>
</div>
</div>
答案 1 :(得分:0)
根据http://guides.rubyonrails.org/routing.html#nested-resources
的Atleast你还需要做
resources :articles do
resources :comments
end
设置此项时会出现哪个错误?还尝试重新启动服务器。