存在的方法的NoMethodError

时间:2016-01-08 19:52:35

标签: ruby-on-rails

嗯,我是铁杆的新手,但不是新的铁路方式,我有一个错误,我不知道如何解决它。

我创建了控制器而不是视图。

控制器:

class ReclamacoesController < ApplicationController

    def new
       @reclamacao = Reclamacao.new
    end
end

以及视图&gt; controllerName&gt; new.html.erb。

下的视图
<%= form_for @reclamacao do |f| %>
    <%= f.text_field  :titulo %>
<% end %>

Reclamacao模型存在。

我也为它创建了资源路径。

resources :reclamacoes

所以,当我访问/ reclamacoes / new时会抛出异常。

NoMethodError in Reclamacoes#new 
undefined method `reclamacaos_path' for #<#<Class:0x00000001fc0660>:0x00000001fba850>

Extracted source (around line #1):
<%= form_for @reclamacao do |f| %>
    <%= f.text_field  :titulo %>
<% end %>

Rails.root: /home/ubuntu/workspace/aqueleprojetoprivate/medicos
Application Trace | Framework Trace | Full Trace

app/views/reclamacoes/new.html.erb:1:in `_app_views_reclamacoes_new_html_erb___3194888715597102324_16164860'

路线:

     reclamacoes GET    /reclamacoes(.:format)          reclamacoes#index
                 POST   /reclamacoes(.:format)          reclamacoes#create
   new_reclamaco GET    /reclamacoes/new(.:format)      reclamacoes#new
  edit_reclamaco GET    /reclamacoes/:id/edit(.:format) reclamacoes#edit
       reclamaco GET    /reclamacoes/:id(.:format)      reclamacoes#show
                 PATCH  /reclamacoes/:id(.:format)      reclamacoes#update
                 PUT    /reclamacoes/:id(.:format)      reclamacoes#update
                 DELETE /reclamacoes/:id(.:format)      reclamacoes#destroy

有什么问题?

2 个答案:

答案 0 :(得分:2)

Rails正试图自动猜测复数。问题是您的资源是reclamacao,其中Rails变为reclamacaos复数。但您已将其命名为reclamacoes

我建议更改名称或指示Rails使用更好的复数。以下是相关文章:How do I override rails naming conventions?

答案 1 :(得分:1)

查看rake routes的输出。您会注意到拼写错误

reclamacoes    GET    /reclamacoes(.:format)          reclamacoes#index
               POST   /reclamacoes(.:format)          reclamacoes#create
new_reclamaco  GET    /reclamacoes/new(.:format)      reclamacoes#new
edit_reclamaco GET    /reclamacoes/:id/edit(.:format) reclamacoes#edit
reclamaco      GET    /reclamacoes/:id(.:format)      reclamacoes#show
               PATCH  /reclamacoes/:id(.:format)      reclamacoes#update
               PUT    /reclamacoes/:id(.:format)      reclamacoes#update
               DELETE /reclamacoes/:id(.:format)      reclamacoes#destroy

根据以上输出,正确的路径名称为reclamacoes path

Rails强调Convention over Configuration,你的模型,视图和控制器中有不同的拼写。