开发者

Rails 3.0.9: abstracting cart routes

开发者 https://www.devze.com 2023-03-26 07:00 出处:网络
I am working on an e-commerce rails app and I am trying to abstract the routes, particularly the ids, of my carts.

I am working on an e-commerce rails app and I am trying to abstract the routes, particularly the ids, of my carts.

Cart Table:

  • id :int, not null, primary key
  • hash_id :string(255)

I need the hash_id to be the only way to render the show action of the carts controller.

I am currently trying to accomplish this using the following code:

#routes.rb
match 'carts/:hash_id' => 'carts#show'

#carts_controller.rb
def show
    @cart = Cart.find(params[:hash_id])
end

Using this code I can still visit example.com/carts/1 and have show work just fine but when I try to visit example.com/carts/hash_id it throws an ActiveRecord::RecordNotFound exception.

So, assuming that there is a cart with :id => 1 and :hash_id => 2414e80f5d9ccaf3 my expected behavior would be example.com/carts/2414e80f5d9ccaf3 would render the show action 开发者_如何学Cof the carts controller for the cart with id of 1.


try @cart = Cart.find_by_hash_id(params[:hash_id])

0

精彩评论

暂无评论...
验证码 换一张
取 消