我的orders_controller.rb
中有一些自定义操作:
class OrdersController < ApplicationController
def process_payment
@order = Order.friendly.find(params[:id])
[snip]
binding.pry_remote # drops into the terminal here for debugging
redirect_to order_path(@order)
[snip]
end
end
Testivate::Application.routes.draw do
resources :orders do
member do
get :pay
post :process_payment
end
end
end
我可以在终端中看到,在@order#process_payment
操作中,friendly_id
正在运行:
[1] pry(#<OrdersController>)> @order
+----+-------+-------+------+------+------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
| id | co... | url | comp | comp | comp | us... | gu... | gu... | cr... | up... | pr... | uuid | sp... | payed |
+----+-------+-------+------+------+------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
| 1 | Th... | ht... | http | http | http | 1 | | | 20... | 20... | 1 | ee... | true | false |
+----+-------+-------+------+------+------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
1 row in set
[2] pry(#<OrdersController>)> @order.uuid
=> "ee1bbe01-e1ca-420c-991b-76ce90525cf8"
[3] pry(#<OrdersController>)> request.original_url
=> "http://127.0.0.1:4000/orders/ee1bbe01-e1ca-420c-991b-76ce90525cf8/process_payment"
[4] pry(#<OrdersController>)> order_path(@order)
=> "/orders/ee1bbe01-e1ca-420c-991b-76ce90525cf8"
但是当@order#process_payment
尝试重定向到@order#show
时,它会失败。
我收到错误:
ActiveRecord::RecordNotFound at /orders/ee1bbe01-e1ca-420c-991b-76ce90525cf8
Couldn't find Order with id=ee1bbe01-e1ca-420c-991b-76ce90525cf8
但是,奇怪的是,这个错误似乎发生在Rails尝试完成@order#show
之前,因为它永远不会落入我的@order#show
行动中的终端:
class OrdersController < ApplicationController
def show
binding.pry_remote # should drop down to terminal here for debugging, but we never get this far
@order = Order.friendly.find(params[:id])
respond_with @order
end
end
为什么会这样?
其他可能相关的代码:
class Order < ActiveRecord::Base
extend FriendlyId
friendly_id :uuid
after_create :generate_uuid
def generate_uuid
self.update_attributes :uuid => SecureRandom.uuid
end
end
使用Rails 4.0.2和friendly_id 5.0.3。
答案 0 :(得分:0)
friendly_id :uuid, use: [:slugged, :finders]
这基本上设置了Rails的默认find
方法,以查找标准id
和slug
。我认为这将解决您的问题