【问题标题】:Ruby on Rails: NoMethodError in Articles#ShowRuby on Rails:文章中的 NoMethodError#Show
【发布时间】:2017-01-12 11:09:17
【问题描述】:

我正在学习http://guides.rubyonrails.org/getting_started.html 教程,使用 Rails 5 和 Ruby 2.4 创建博客。在将我的方式复制并粘贴到第 6 单元:添加评论模型的末尾之后,Rails 抛出了这个错误:

“Articles#Show 中的 NoMethodError”:未定义的方法 #

<h2>Add a comment:</h2>
<%= form_for([@article, @article.comments.build]) do |f| %><!--****Error?****-->

2014 年 10 月 26 日的 Stackoverflow 回答说要向 routes.rb 添加 article_cmets_path 辅助方法,如下所示:

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

我没有在终端中发现任何拼写错误,所以我不确定如何继续。非常感谢任何指导。

我的 'rake 路线 | grep cmets 的输出是:

comments GET    /comments(.:format)          comments#index
         POST   /comments(.:format)          comments#create

new_comment GET /cmets/new(.:format) cmets#new edit_comment GET /cmets/:id/edit(.:format) cmets#edit 评论 GET /cmets/:id(.:format) cmets#show PATCH /cmets/:id(.:format) cmets#update PUT /cmets/:id(.:format) cmets#update 删除 /cmets/:id(.:format) cmets#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

有什么问题吗?

【问题讨论】:

  • 只需在终端 'rake routes | grep cmets'
  • nested routes 的语法仍然相同。它试图调用什么方法?
  • 我的 rake 路线 | grep cmets 输出为: cmets GET /cmets(.:format) cmets#index POST /cmets(.:format) cmets#create new_comment GET /cmets/new(.:format) cmets#new edit_comment GET /cmets/:id/ edit(.:format) cmets#edit comment GET /cmets/:id(.:format) cmets#show PATCH /cmets/:id(.:format) cmets#update PUT /cmets/:id(.:format) cmets #update DELETE /cmets/:id(.:format) cmets#destroy

标签: ruby-on-rails ruby nested-attributes


【解决方案1】:

至少根据http://guides.rubyonrails.org/routing.html#nested-resources

你还需要做

resources :articles do
  resources :comments
end

当你设置这个时你会得到什么错误?也尝试重新启动您的服务器。

【讨论】:

  • 我遇到了同样的错误,检查一下我发现我错过了这一步。谢谢。
【解决方案2】:

根据您提供的文档,您应该为嵌套路由使用块:

Rails.application.routes.draw do
resources :articles do
  resources :comments
end
root 'welcome#index'

Ruby 不知道这种缩进,因此格式正确的代码如下所示:

Rails.application.routes.draw do
  resources :articles
  resources :comments#This creates comments as a nested resource within articles.
  root 'welcome#index
end

编辑:您始终可以使用rake routes 在 cli 中检查您的路线

【讨论】:

  • 我的 routes.rb 缩进就像你的例子,我的 rake 路由似乎正确对应。
  • 好的。只需按照文档中的说明进行操作(请参阅我的第一个代码块),您就可以开始了 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多