# Passing a value from form to a model?

**URL:** <https://www.ruby-forum.com/t/passing-a-value-from-form-to-a-model/136148>\
**Category:** Rails\
**Created:** [April 29, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/passing-a-value-from-form-to-a-model/136148 "2008-04-29T12:16:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [April 29, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/passing-a-value-from-form-to-a-model/136148/1 "2008-04-29T12:16:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [April 29, 2008, 12:23pm UTC](https://www.ruby-forum.com/t/passing-a-value-from-form-to-a-model/136148/2 "2008-04-29T12:23:10Z")

</div>

On 29 Apr 2008, at 11:15, AN@S wrote:

> Pin Code:  
> \<%= f.text\_field :card, :size =\> 13%\>

Because you are using form\_for, that parameter will be available as  
params[:user][:card], not params[:card]

Fred
