我一直在 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