我有两个嵌套资源:
class Customer < ActiveRecord::Base
has_many :locations, :dependent => :destroy
accepts_nested_attributes_for :locations
end
class Location < ActiveRecord::Base
belongs_to :customer
end
在路线。我有
resources :customers do
resources :locations
end
创建新位置的代码为
<%= link_to image_tag("add.png"), new_customer_location_path(@customer), :class => "button" %>
当我尝试创建一个新位置时,我得到以下错误
路由错误没有匹配的路由{:action =>"显示":控制器=>"地点":customer_id => #: id => #}
为什么调用:action=>"show"而不是"new"?
rake路由输出
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
我不明白这个错误,我有另外两个嵌套的资源,工作正常,检查了所有的代码,它似乎完全相同…有没有可能"位置"是一个保留词,或者有什么遗漏/错误?
好的,找到问题了。
在app/views/位置/_form.html.erb它被app/views/locations/new.html.erb
调用有一个路径错误的链接帮助:
<%= link_to "Cancel", customer_location_path(@customer,@location) %>
我已经把它改成
<%= link_to "Cancel", customer_path(@customer) %>
现在一切正常
试着在image_tag上加一个右括号。
<%= link_to image_tag("add.png"), new_customer_location_path(@customer), :class => "button" %>