调用“new”的路由错误具有嵌套资源的方法

发布于 2024-12-22 03:54:31 字数 2059 浏览 2 评论 0原文

我有两个嵌套资源:

class Customer < ActiveRecord::Base
  has_many :locations, :dependent => :destroy
  accepts_nested_attributes_for :locations
end

class Location < ActiveRecord::Base
  belongs_to :customer
end

在 paths.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"?

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

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

I have two nested resources:

class Customer < ActiveRecord::Base
  has_many :locations, :dependent => :destroy
  accepts_nested_attributes_for :locations
end

class Location < ActiveRecord::Base
  belongs_to :customer
end

In routes.rb I have

resources :customers do
  resources :locations
end    

The code for creating a new location is

<%= link_to image_tag("add.png"), new_customer_location_path(@customer), :class => "button" %>

When I try to create a new location I get the following error

Routing Error

No route matches {:action=>"show", :controller=>"locations", :customer_id=>#, :id=>#}

Why :action=>"show" is invoked instead of "new"?

rake routes output is

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

The controller code for the new action in locations_controller.rb is

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

I do not understand the error, I have another two nested resources that work correctly, checked all the code and it seems exactly the same...
Is there a possibility that "location" is a reserved word, or is there something missing/wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿萝 2024-12-29 03:54:31

好的,找到问题了。

内部 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) %>

,现在一切正常

Ok, found the problem.

Inside app/views/locations/_form.html.erb
which is invoked by app/views/locations/new.html.erb

there was a link with a wrong path helper:

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

I have changed it to

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

and now everything works

最单纯的乌龟 2024-12-29 03:54:31

尝试在 image_tag 上放置右括号。

<%= link_to image_tag("add.png"), new_customer_location_path(@customer), :class => "button" %>

Try putting a closing bracket on the image_tag.

<%= link_to image_tag("add.png"), new_customer_location_path(@customer), :class => "button" %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文