【发布时间】:2011-10-08 13:43:56
【问题描述】:
这是在我的 routes.rb 文件中:
resources :orders do
member do
get 'confirm'
get 'cancel'
end
end
当然,我在 orders_controller.rb 中编写了“确认”和“取消”操作:
def confirm
@order = Order.find(params[:id])
#...
end
def cancel
@order = Order.find(params[:id])
#...
end
..并在我的“app/views/orders/”文件夹中创建了“confirm.html.erb”和“cancel.html.erb”文件。
但是当我尝试访问 confirm_order_url 或 cancel_order_url 时,我总是遇到这个路由错误:
No route matches {:action=>"confirm", :controller=>"orders"}
无法弄清楚缺少什么! 请问有什么办法吗?
PS:我正在使用 RoR v.3.1.0
编辑#1: 似乎路线设置正确:
$ rake routes
[..]
confirm_order GET /orders/:id/confirm(.:format) {:action=>"confirm", :controller=>"orders"}
cancel_order GET /orders/:id/cancel(.:format) {:action=>"cancel", :controller=>"orders"}
orders GET /orders(.:format) {:action=>"index", :controller=>"orders"}
POST /orders(.:format) {:action=>"create", :controller=>"orders"}
new_order GET /orders/new(.:format) {:action=>"new", :controller=>"orders"}
edit_order GET /orders/:id/edit(.:format) {:action=>"edit", :controller=>"orders"}
order GET /orders/:id(.:format) {:action=>"show", :controller=>"orders"}
PUT /orders/:id(.:format) {:action=>"update", :controller=>"orders"}
DELETE /orders/:id(.:format) {:action=>"destroy", :controller=>"orders"}
[...]
编辑#2: 也许当我调用confirm_order_url Rails 不知道如何生成应该类似于“orders/:id/confirm”的路由,因为@order.id 尚未分配。 但是我在@order.save 之后调用了这个方法。 即,这里:
if @order.save
response = PAYPAL_EXPRESS_GATEWAY.setup_purchase(@order.price_in_cents,
:ip => @order.ip_address,
:return_url => confirm_order_url,
:cancel_return_url => cancel_order_url
)
redirect_to PAYPAL_EXPRESS_GATEWAY.redirect_url_for(response.token)
所以这导致了一个子问题:@order.id 是在调用@order.save 之后设置的吗? 如果没有,在调用诸如广告确认订单网址之类的辅助方法之前如何正确设置它?
解决方案 感谢 Jayendra Patil,我以这种方式修复了我的代码:
if @order.save
response = PAYPAL_EXPRESS_GATEWAY.setup_purchase(@order.price_in_cents,
:ip => @order.ip_address,
:return_url => confirm_order_url(@order),
:cancel_return_url => cancel_order_url(@order)
)
redirect_to PAYPAL_EXPRESS_GATEWAY.redirect_url_for(response.token)
我错误地假设 Rails 可以猜测我为哪个 @order 调用“成员”url,所以答案是传递 @order 作为成员路由的参数。谢谢。
【问题讨论】:
-
请发
rake routes的内容 -
我是根据你在“EDIT#1”下的建议做的,ty.
标签: ruby-on-rails ruby-on-rails-3 routes ruby-on-rails-3.1