我有两个嵌套资源:
class Customer < ActiveRecord::Base
has_many :locations, :dependent => :destroy
accepts_nested_attributes_for :locations
end
class Location < ActiveRecord::Base
belongs_to :customer
end
在 routes.rb 我有
resources :customers do
resources :locations
end
创建新位置的代码是
<%= link_to image_tag("add.png"), new_customer_location_path(@customer), :class => "button" %>
当我尝试创建新位置时,出现以下错误
路由错误
没有路线匹配 {:action=>"show", :controller=>"locations", :customer_id=>#, :id=>#}
为什么调用 :action=>"show" 而不是 "new"?
耙路线输出是
customer_locations GET /customers/:customer_id/locations(.:format) {:action=>"index", :controller=>"locations"}
POST /customers/:customer_id/locations(.:format) {:action=>"create", :controller=>"locations"}
new_customer_location GET /customers/:customer_id/locations/new(.:format) {:action=>"new", :controller=>"locations"}
edit_customer_location GET /customers/:customer_id/locations/:id/edit(.:format) {:action=>"edit", :controller=>"locations"}
customer_location GET /customers/:customer_id/locations/:id(.:format) {:action=>"show", :controller=>"locations"}
PUT /customers/:customer_id/locations/:id(.:format) {:action=>"update", :controller=>"locations"}
DELETE /customers/:customer_id/locations/:id(.:format) {:action=>"destroy", :controller=>"locations"}
locations_controller.rb 中新操作的控制器代码是
before_filter :find_customer
def new
@location = @customer.locations.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @location }
end
end
我不明白这个错误,我还有另外两个可以正常工作的嵌套资源,检查了所有代码,它看起来完全一样......“位置”是否有可能是保留字,或者是否有遗漏/错误?