Rails 3.0.9: abstracting cart routes

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 of the carts controller for the cart with id of 1.

-------------Problems Reply------------

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

Category:ruby on rails Views:0 Time:2011-08-04

Related post

  • AJAX + RAILS Problem with ShowTooltip window effect-Routing Error in Rails 2010-02-13

    AJAX + RAILS Question. Am getting a Routing error for the following: <div class="card-field"> <h6><imgsrc="/images/red_icon.png" width="16" alt="" align="absmiddle" class="image-3" onmouseover="ajax_showTooltip(window.event,'/quickadd_

  • rails 3.1 ActionController::RoutingError (No route matches [GET] "/assets/rails.png"): 2011-08-18

    Standard new rails app has issue showing the rails.png ActionController::RoutingError (No route matches [GET] "/assets/rails.png"): I have tried moving the .png file around to various places in assets and assets/images and also the older place 'publi

  • Rails 2.3.5 Creating RESTful routes for a model named "Software" 2010-02-17

    Topic says it all guys, my program needs to be able to keep track of the "software" that belongs to a user, as such I have a MVC for "Software". However, I can't use RESTful routes at the moment since my plural and singular names are the same, it's "

  • New to Rails. Doubt in Big URL Routing 2010-05-31

    I have just started learning ruby on rails. I have a doubt wrt routing. Default Routing in Rails is :controller/:action/:id It works really fine for the example lets say example.com/publisher/author/book_name Could you tell me how do you work with so

  • Rails Functional test on a custom route 2010-06-17

    I have the following routes in my app: GET /admin/comments(.:format) {:controller=>"admin/comments", :action=>"index"} admin_comments POST /admin/comments(.:format) {:controller=>"admin/comments", :action=>"create"} new_admin_comment GET

  • Rails 2.3.8: namespace + default route (server-dependent routing issue) 2010-08-30

    I have FriendshipRequests controller under controllers/users/ namespace. The problem appeared after setting up stage server: same url is processed different locally and on the stage. locally (mac os X, ruby 1.8.7p174, frozen rails 2.3.8, rack 1.1.0)

  • Rails: what is wrong with this route? 2010-09-22

    For some strange reason cardsets_path('my') generates "/cardsets.my" instead of "/cardsets/my". Please explain why? config/routes.rb: match '/cardsets/:type', :to => 'cardsets#index', :requirements => { :type => /my|public/ }, :as => 'car

  • Ruby on Rails: How do you prefix named routes? 2010-12-17

    I'm looking to generate a link that has a prefix attached to the named route itself. Something like this to display the path "/old/recipes": recipes_path(:prefix => "old/") # the correct way should show "/old/recipes" I don't want to touch the rou

  • Ruby on Rails: how does one write a route such that id's are ignored? 2010-12-27

    the default route is :controller/:action/:id but I want just :controller/:action for my errors controller. I kinda have this: but it doesn't work, as .connect needs an action map.connect 'errors/:action/', :controller => :errors currently, when I

  • Rails help with building Admin area - Routing problem 2011-03-03

    I am making a Admin area. In app/controllers application_controller.rb public_controller.rb admin_controller.rb kategoris_controller.rb And in app/controllers/admin kategoris_controller.rb My route file: resources :kategoris namespace :admin do resou

  • Rails 3, Polymorphic Associations, and No Route Matches 2011-04-20

    I've been learning Rails for about 6 weeks now, so still a noob! I'm following Ryan Bates screencast on Polymorphic Associations, but I'm getting a "No Route Matches" error when navigating to /model/xx/comments. After two days going round in circles

  • Rails: Different Controller behaviour depending on route 2011-05-10

    I'm looking for the best practice to solve the following situation: I've got an "Additive" Model which should be many-to-many-associated with some other models. Examples: # Meal-Model has_and_belongs_to_many :additives # Offer-Model has_and_belongs_t

  • What will my Delete helper - in Rails 3- look like for a route that's nested? 2011-09-12

    When I just had resources :feedback in my routes.rb, I would use this link helper that would work fine: <%= link_to 'Delete', feedback, :confirm => 'Are you sure?', :method => :destroy %> But now, given that I have updated the route to be

  • Rails how to rewrite this old route map.connect? 2011-09-15

    How do I rewrite this old route Rails 1.2.6 to Rails 3? : # Allow downloading Web Service WSDL as a file with an extension # instead of a file named 'wsdl' map.connect ':controller/service.wsdl', :action => 'wsdl' I can´t see how I should use matc

  • Rails help creating redirecting action and route 2011-09-15

    I am trying to create a route and a action that redirects the params. Example when a user visits: www.mywebsite.com/photographer/flv/:ID/:filename I want the user to be redirected to: www.someotherwebsite.com/photographer/flv/:ID/:filename I have tri

  • Rails can't match the existed route 2011-10-01

    First I rake routes to check all the routes and make sure it exists in my app. In my route.rb resources :user do resource :account resource :addresses end And now everything is fine so far. I got some path helper method. Such as user_addresses_path t

  • No "(.:format)" in Rails 3.1 when trying to route site root 2011-10-07

    Since upgrading to Rails 3.1, I've been having a problem with my site's routing. Previously, with Rails 3.0, I was able to do this in my router config: resources :quotes, :path => "" root :to => "quotes#index" That would give me routes like GET

  • Rails 3: making a catch-all route easier to read and amend 2011-10-07

    I'm trying to write a catch-all route in Rails 3, but I want to reserve some terms in it. I'm specifically following the example put forth in this post, in the answer by David Burrows: Dynamic routes with Rails 3 The syntax I am using is the followin

  • Rails 3.1 empty parameters from routes 2011-11-01

    I have migrated a project from Rails 2.3 to Rails 3.1 and I have the following issue. Using any of these routes configs: match 'help/*details' => 'help#index' # or match ':controller/:action/:id' The matching works, but inside the specific action

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.199 (s). 11 q(s)