Hello, I’m writing an application that has a user registration functionality, the sign up form has a field that is called ‘card’, here the user is supposed to enter a valid PIN code. PIN codes are stored in a table called Cards, each card has a PIN and a serial number. If the PIN is correct and available in the Cards table registration will be completed successfully, else registration will fail. I’m relatively new to rails and there still some concepts that are not clear to me, this is one of them. My problem is with the PIN code validation as described below:
#Viewer:
Signup
<%= error_messages_for :user %> <% form_for :user, :url => users_path do |f| -%>Username:
<%= f.text_field :username, :size => 25 %>
Email:
<%= f.text_field :email, :size => 25 %>
Password:
<%= f.password_field :password, :size => 25 %>
Password Confirmation:
<%= f.password_field :password_confirmation, :size => 25 %>
Mobile Number:
<%= f.text_field :mobile_number, :size => 13%>
Pin Code:
<%= f.text_field :card, :size => 13%>
<%= submit_tag ‘Sign Up’ %> <% end -%>
#Model: User.rb has_many :cards
def self.available_pin?(card) Card.find_by_pin(card) ? true : false end
#Controller: users_controller.rb
def create if User.available_pin?(params[:card])
@user = User.new(params[:user])
if @user.save
self.logged_in_user = @user
flash[:notice] = "Your account has been created."
redirect_to messages_url
else
render :action => 'new'
end
else
flash[:notice] = "PIN is wrong"
end
end
When I try the registration I always get the flash message that says: ‘PIN is wrong’ even when I try an available PIN.
Thanks in advance.