Question

I have some custom actions in my 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

I can see in the terminal that, within the @order#process_payment action, friendly_id is working:

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

But when @order#process_payment attempts to redirect to @order#show, it fails.

I get the error:

ActiveRecord::RecordNotFound at /orders/ee1bbe01-e1ca-420c-991b-76ce90525cf8
Couldn't find Order with id=ee1bbe01-e1ca-420c-991b-76ce90525cf8

But, strangely, this error seems to occur before Rails attempts to complete @order#show, as it never drops down to the terminal within my @order#show action:

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

Why is this?

Other potentially relevant code:

class Order < ActiveRecord::Base
  extend FriendlyId
  friendly_id :uuid
  after_create :generate_uuid  
  def generate_uuid
    self.update_attributes :uuid => SecureRandom.uuid
  end
end

Using Rails 4.0.2 and friendly_id 5.0.3.

Was it helpful?

Solution

You'll want to look at this:

friendly_id :uuid, use: [:slugged, :finders]

This basically sets the default find method of Rails to look for both the standard id and the slug. I think this will solve your problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top