0

我一直在 Rails 中为这种事情苦苦挣扎。我正在尝试为给定城市创建统计记录。我想我不需要粘贴我的模型?下面是错误、URL、视图和控制器代码。

这是我的错误:

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

我要走的路是:

http://127.0.0.1:3000/cities/1/statistics/new

路线:

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

看法:

%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"

编辑添加 routes.rb

4

2 回答 2

1

两件事情。

  1. 正如@nathan 指出英语中城市的复数形式是城市,所以你的路线有问题,也许你重命名了它?(请包括routes.rb
  2. 您有一条静态路线,它是城市的嵌套资源,因此对于表单助手,您应该通过

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

于 2013-07-12T19:15:36.190 回答
-1

您的路径文件显示citys而不是cities. 也许您被 ActiveResource 的复数问题所困扰?

于 2013-07-12T19:11:13.473 回答