views:

51

answers:

2

i have a codes model and i a basically have a form wich should authenticate simply to every code recorded so my controller is:

def cancel_sale

  @codes = Code.find(:all)

  @codes.each do |c|
    @code = c.name
  end

  if params[:auth] && params[:auth] == @code

    something 

  else 

    @mensaje_de_salida = "wrong auth code"

  end

end

i know it's bad, at this moment it only authenticates to the last recorded code.

thanks in advance.

A: 

What about this?

def cancel_sale

  @codes = Code.find(:all)

  if params[:auth] && @codes.include?(params[:auth])
    #something
  else 
    @mensaje_de_salida = "wrong auth code"
  end

end
Gabe Hollombe
A: 

Try this. Gabe's method is correct as well if you actually want to use @codes for another purpose otherwise no need to retreive all the codes.

@codes = lambda { Code.find(:all, :conditions => ["auth = ?", params[:auth] ] ) }

if params[:auth] && !(@codes.call.empty?)
   #something
else
   #nothing
end
andHapp