Ruby on rails 进入页面创建新记录时出现NoMethodError

Ruby on rails 进入页面创建新记录时出现NoMethodError,ruby-on-rails,ruby-on-rails-3,routes,Ruby On Rails,Ruby On Rails 3,Routes,在Rails中,我一直在努力解决这类问题。我正在尝试为给定的城市创建一个统计记录。我想我不需要粘贴我的模型了?下面是错误、URL、视图和控制器代码 这是我的错误: NoMethodError in Statistics#new Showing new.html.haml where line #2 raised: undefined method `statistics_path' for #<#<Class:0x007faf5e172928>:0x007faf5e717ef0

在Rails中,我一直在努力解决这类问题。我正在尝试为给定的城市创建一个统计记录。我想我不需要粘贴我的模型了?下面是错误、URL、视图和控制器代码

这是我的错误:

NoMethodError in Statistics#new
Showing new.html.haml where line #2 raised:
undefined method `statistics_path' for #<#<Class:0x007faf5e172928>:0x007faf5e717ef0>
路线:

city_statistics GET    /cities/:city_id/statistics(.:format)          statistics#index
                         POST   /cities/:city_id/statistics(.:format)          statistics#create
  new_city_statistic GET    /cities/:city_id/statistics/new(.:format)      statistics#new
 edit_city_statistic GET    /cities/:city_id/statistics/:id/edit(.:format) statistics#edit
      city_statistic GET    /cities/:city_id/statistics/:id(.:format)      statistics#show
                         PUT    /cities/:city_id/statistics/:id(.:format)      statistics#update
                         DELETE /cities/:city_id/statistics/:id(.:format)      statistics#destroy
               cities GET    /cities(.:format)                                  cities#index
                         POST   /cities(.:format)                                  cities#create
            new_city GET    /cities/new(.:format)                              cities#new
           edit_city GET    /cities/:id/edit(.:format)                         cities#edit
                city GET    /cities/:id(.:format)                              cities#show
                         PUT    /cities/:id(.:format)                              cities#update
                         DELETE /cities/:id(.:format)                              cities#destroy

routes.rb:

resources :cities do
   resources :statistics
end

routes.rb:
控制器:

def new
  @statistic = Statistic.new

  respond_to do |format|
    format.html # new.html.haml
    format.json { render :json => @statistic }
  end
end
视图:


EDITadded routes.rb

您的路径文件显示的是
城市
,而不是
城市
。也许你被ActiveResource多元化问题困扰了?

两件事

  • 英语中,@nathan points的复数形式是cities,所以你们的路线有问题,也许你们把它改名了?(请包括
    routes.rb
  • 您有一个static的路由,它是城市的嵌套资源,所以对于表单助手,您应该传递它

  • =form_for([@city,@statistic])do | f |

    不介意否决票,但请解释它是如何不正确的。在我的回答中解释。投票被否决只是为了秩序,所以我的答案被问题的作者看到了。请添加你的routes.rb文件,根据你应该使用的路线,[@city,@statistic]@Michael Szyndel:谢谢。更改了这两个内容,但现在我得到了:
    未定义的方法
    模型名称'NilClass:Class`您必须先在控制器中设置@city!哦,谢谢:)!!你的建议帮助我解决了这个问题。非常感谢。
    def new
      @statistic = Statistic.new
    
      respond_to do |format|
        format.html # new.html.haml
        format.json { render :json => @statistic }
      end
    end
    
    %legend New Stat
    = form_for(@statistic) do |f| ###### ERROR HERE #######
      = f.label :city_id
      = f.text_field :city_id
      .actions
        = f.submit "Add", :id => "add-statistic", :class => "btn btn-primary"