0

we have an show-action which should find by the own_key the right entry. The own_key is generated as UUIDTools::UUID.timestamp_create().to_s. The following question is now here.

class ListController < ApplicationController
  respond_to :html, :xml, :json
  def show
    @list = List.find_by_own_key(params[:own_key])
    respond_with(@list)
  end
end

the routes are here so generates

resources :lists
match '/:id' => 'list#show'

why did we get also an entry back if we only type one simple letter after the /? The own_key look so f6d47c20-a276-11e1-b127-68a3c454c2b4. So if we type an /lists/f i get the entry with an f own_key. how can we manage that we only get the entry with the own_key? Could it run by an contraint?

thanks for the help if anone can help us?

Marcus

amarradi
  • 109
  • 3
  • 16
  • So is there an entry whose key is exactly 'f' or not? – Frederick Cheung May 20 '12 at 17:56
  • No the key are never only one letter, they were always generated by UUID tool. thats our problem that the filtering is not correct – amarradi May 20 '12 at 18:00
  • That doesn't sound like it should happen. Worth am checking what SQL gets executed. – Frederick Cheung May 20 '12 at 18:04
  • That is the output from the development.log `Started GET "/lists/f6d47c20-a276-11e1-b127-68a3c454c2b4" for 127.0.0.1 at 2012-05-20 20:17:08 +0200 Processing by ListsController#show as HTML Parameters: {"id"=>"f6d47c20-a276-11e1-b127-68a3c454c2b4"} List Load (0.3ms)[0m [1mSELECT "lists".* FROM "lists" WHERE "lists"."admin_key" IS NULL LIMIT 1[0m Rendered lists/show.html` – amarradi May 20 '12 at 18:22
  • Now i get an other problem. i always get the same entry... with this show-method – amarradi May 20 '12 at 18:34

2 Answers2

1

From your routes, params[:id] will contain the I'd to search for, however you're using params[:own_key] which will be nil. Instead of searching for the record with the specified value of own_key your code will always fetch the row with a null own_key.

Change your code to use params[:id] and you should be ok

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
0

U can use constrainsts for route or check params[:own_key] in controller

:constraints => {:own_key=> /regexp pattern for uuid/}

Yuri Barbashov
  • 5,407
  • 1
  • 24
  • 20