资源不足的路由中的路由错误 "No route matches"
Routing error "No route matches" in non-resourceful route
我已经设置了一条我认为应该可行的路由,但却给我以下路由错误:
No route matches {
:action=>"show", :controller=>"orders",
:id=>#<Order id: nil,
user_id: nil,
paid: nil,
created_at: nil,
updated_at: nil,
trip_id: nil
>
}
我正在使用 Rails 版本 3.2.13。
在 routes.rb
我有 get 'orders/new/:trip_id' => 'orders#new', :as => :new_order
。这出现在 routes.rb
中的 resources :orders
之前。
在视图中,我有<%= link_to 'Click here to register for this trip.', new_order_path(@trip.id) %>
。在 @trip.id = 1
的情况下,这会产生以下(似乎是正确的)HTML:<a href="/orders/new/1">Click here to register for this trip.</a>
.
当我 运行 rake:routes
时,这一行(看似正确)在生成的行中:new_order GET /orders/new/:trip_id(.:format) orders#new
.
来自 rake:routes
的其他与订单相关的路线是
orders GET /orders(.:format) orders#index
POST /orders(.:format) orders#create
GET /orders/new(.:format) orders#new
edit_order GET /orders/:id/edit(.:format) orders#edit
order GET /orders/:id(.:format) orders#show
PUT /orders/:id(.:format) orders#update
DELETE /orders/:id(.:format) orders#destroy
在 orders_controller.rb
中,以下行作为我的新方法的第一行出现:@trip = Trip.find(params[:trip_id])
.
在控制台中,当我尝试 Rails.application.routes.recognize_path "/orders/new/1"
时,我得到 => {:controller=>"orders", :action=>"new", :trip_id=>"1"}
,这似乎是正确的。好郁闷
我不明白的是,为什么当我点击 link 时,会出现上面第一段中显示的路由错误。 有趣的是,当我在 routes.rb
中注释掉 resources :orders
时,将调度预期的操作(即 OrdersController.new
)... 但是,当然,我失去了我在别处使用的足智多谋的路由。请帮忙!
你不能这样添加路由
get 'orders/new/:trip_id' => 'orders#new', :as => :new_order
Rails 将此理解为显示路径
而是像这样添加
get 'orders/:trip_id/new/' => 'orders#new', :as => :new_order
最后还是自己解决了这个问题,觉得没有早点看到有点傻。我忘记了我已经引入了相关视图使用的新布局。此布局包括 link_to order_path(@order) if @order
。事实上,这是导致错误的行。 @order
存在,但还没有持久化,所以@order.id
不存在;这阻止了它匹配 @order
: order GET /orders/:id(.:format) orders#show
的足智多谋的 show 路由并导致错误。 我通过将 link_to order_path(@order) if @order
更改为 link_to order_path(@order) if @order.id
来解决问题。
我已经设置了一条我认为应该可行的路由,但却给我以下路由错误:
No route matches {
:action=>"show", :controller=>"orders",
:id=>#<Order id: nil,
user_id: nil,
paid: nil,
created_at: nil,
updated_at: nil,
trip_id: nil
>
}
我正在使用 Rails 版本 3.2.13。
在 routes.rb
我有 get 'orders/new/:trip_id' => 'orders#new', :as => :new_order
。这出现在 routes.rb
中的 resources :orders
之前。
在视图中,我有<%= link_to 'Click here to register for this trip.', new_order_path(@trip.id) %>
。在 @trip.id = 1
的情况下,这会产生以下(似乎是正确的)HTML:<a href="/orders/new/1">Click here to register for this trip.</a>
.
当我 运行 rake:routes
时,这一行(看似正确)在生成的行中:new_order GET /orders/new/:trip_id(.:format) orders#new
.
来自 rake:routes
的其他与订单相关的路线是
orders GET /orders(.:format) orders#index
POST /orders(.:format) orders#create
GET /orders/new(.:format) orders#new
edit_order GET /orders/:id/edit(.:format) orders#edit
order GET /orders/:id(.:format) orders#show
PUT /orders/:id(.:format) orders#update
DELETE /orders/:id(.:format) orders#destroy
在 orders_controller.rb
中,以下行作为我的新方法的第一行出现:@trip = Trip.find(params[:trip_id])
.
在控制台中,当我尝试 Rails.application.routes.recognize_path "/orders/new/1"
时,我得到 => {:controller=>"orders", :action=>"new", :trip_id=>"1"}
,这似乎是正确的。好郁闷
我不明白的是,为什么当我点击 link 时,会出现上面第一段中显示的路由错误。 有趣的是,当我在 routes.rb
中注释掉 resources :orders
时,将调度预期的操作(即 OrdersController.new
)... 但是,当然,我失去了我在别处使用的足智多谋的路由。请帮忙!
你不能这样添加路由
get 'orders/new/:trip_id' => 'orders#new', :as => :new_order
Rails 将此理解为显示路径 而是像这样添加
get 'orders/:trip_id/new/' => 'orders#new', :as => :new_order
最后还是自己解决了这个问题,觉得没有早点看到有点傻。我忘记了我已经引入了相关视图使用的新布局。此布局包括 link_to order_path(@order) if @order
。事实上,这是导致错误的行。 @order
存在,但还没有持久化,所以@order.id
不存在;这阻止了它匹配 @order
: order GET /orders/:id(.:format) orders#show
的足智多谋的 show 路由并导致错误。 我通过将 link_to order_path(@order) if @order
更改为 link_to order_path(@order) if @order.id
来解决问题。