2

I have a form in which a user ('member') submits a 4-digit pin, which is then saved to a session variable called :submitted_pin. For some reason, the quick if/else statement isn't working properly, and I am assuming it is a silly mistake on my part, but if your willing to take a look I would really appreciate it!

View

<%= form_for :member_pin, :url => { :action => "verify_pin", :id => member.id } do |f| %>`
  <%= f.label :pin %>
  <%= f.text_field :pin %>
<%= f.submit 'Submit' %>

Controller

Before Filter

before_filter :validate_pin, :only => :show

Action (POST Route)

def verify_pin
  @member = Member.find(params[:id])

  session[:submitted_pin] = params[:member_pin][:pin]
  redirect_to @member
end

Filter

def validate_pin
  @member = Member.find(params[:id])
  @member_pin = @member.pin

  if session[:submitted_pin] == @member_pin
    render @member
  else
    redirect_to '/'
  end
end

And the result of all of this is a redirect to my root_url no matter what, even if the Pin entered does match the pin in the database for that user. Help! Thanks :)

4
  • In your validate_pin method, right above your if statement, add this line: raise "#{@member_pin} == #{session[:submitted_pin]}: #{@member_pin == session[:submitted_pin]}" and that will give you three pieces of data: the session variable submitted_pin as understood by Rails, the member.pin as understood by Rails, and their equivalency. If I had to venture a guess from a standing start, I'd bet that one is a string and the other is an integer - that screws me up all the time. Commented Feb 15, 2012 at 1:40
  • Thank you so much for your response. It looks like you're right... one must be a string and one must be an integer. This is what I am getting from the raise method you wrote me! 5555 == 5555: false -- How would I go about converting to_i or to_s in my code I provided in my question? Commented Feb 15, 2012 at 1:57
  • Perfect! There's a couple ways to go from here: the simplest is to set .to_i on each of the items you're comparing, that way you don't have to care which is which. The other method would be to use .inspect or .class on each of your data points, to figure out which piece is what, and then use a converter such as .to_i to convert only the one you need. One of those ought to do the trick! Commented Feb 15, 2012 at 2:00
  • Best way to troubleshoot this is by writing a controller test. Commented Feb 15, 2012 at 5:00

1 Answer 1

2

Based on the results of the raise we talked about in the comments, it sounds like there's a class mismatch. The easiest way to fix (and, IMHO, the easiest to read) would be to adjust your validator code as follows:

if session[:submitted_pin].to_i == @member_pin.to_i
  render @member
else
  redirect_to '/'
end

If there's any chance that session[:submitted_pin] would be nil, you can use session[:submitted_pin].try(:to_i); that'll return nil if the variable is not set and prevent an error from getting thrown.

Sign up to request clarification or add additional context in comments.

2 Comments

Yikes! I got this error: undefined method 'pin' for 5555:Fixnum
I should also mention -- that error is being called on the <%= f.text_field :pin %> part of my View. Any ideas? :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.