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 :)
validate_pinmethod, 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.raisemethod you wrote me!5555 == 5555: false-- How would I go about convertingto_iorto_sin my code I provided in my question?.to_ion 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.inspector.classon each of your data points, to figure out which piece is what, and then use a converter such as.to_ito convert only the one you need. One of those ought to do the trick!