2

我有两个嵌套资源:

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

我不明白这个错误,我还有另外两个可以正常工作的嵌套资源,检查了所有代码,它看起来完全一样......“位置”是否有可能是保留字,或者是否有遗漏/错误?

4

2 回答 2

1

好的,找到问题了。

在 app/views/locations/_form.html.erb 内部,由 app/views/locations/new.html.erb 调用

有一个错误路径助手的链接:

    <%= link_to "Cancel", customer_location_path(@customer,@location) %>

我已将其更改为

    <%= link_to "Cancel", customer_path(@customer) %>

现在一切正常

于 2011-12-22T04:26:44.013 回答
0

尝试在 image_tag 上放置一个右括号。

<%= link_to image_tag("add.png"), new_customer_location_path(@customer), :class => "button" %>
于 2011-12-21T13:18:44.077 回答