调用“new”的路由错误具有嵌套资源的方法
我有两个嵌套资源:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,找到问题了。
内部 app/views/locations/_form.html.erb
由 app/views/locations/new.html.erb 调用,
其中存在一个带有错误路径助手的链接:
我已将其更改为
,现在一切正常
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:
I have changed it to
and now everything works
尝试在 image_tag 上放置右括号。
Try putting a closing bracket on the image_tag.