# Read params from form ot working

**URL:** <https://www.ruby-forum.com/t/read-params-from-form-ot-working/74285>\
**Category:** Rails\
**Created:** [November 3, 2006, 1:47pm UTC](https://www.ruby-forum.com/t/read-params-from-form-ot-working/74285 "2006-11-03T13:47:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [November 3, 2006, 1:47pm UTC](https://www.ruby-forum.com/t/read-params-from-form-ot-working/74285/1 "2006-11-03T13:47:39Z")

</div>

hi all,

can someone tell me why I need to get the data from a form like the code  
below  
even when I dont want to create a NEW user?? I am confused

```
@user = User.new(params[:user])
username = @user.username
user = User.find(:first, :conditions => ["username = ?", username])

```

why I cant simply get the form data with  
username = params[:username]

and why needs User.find an ID - also when I say "conditions =\>  
[“username = ?”, username])

what I am doing wrong?  
thanks  
andreas

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [November 3, 2006, 2:48pm UTC](https://www.ruby-forum.com/t/read-params-from-form-ot-working/74285/2 "2006-11-03T14:48:23Z")

</div>

You \>don’t\< have to do it that way. The params[:user] variable in your  
controller is a hash of the form data. Use this:

username = params[:user][‘username’]

As for what you are trying to do, it is much easier than what you are  
trying. Rails creates dynamic finders for your models, so if you have a  
field “username” there is a finder defined as “find\_by\_username”. You  
can use that with the params value to get your user object in one line:

user = User.find\_by\_username(params[:user][‘username’])

Enjoy.  
c.

Andreas S. wrote:

> hi all,
> 
> can someone tell me why I need to get the data from a form like the code  
> below  
> even when I dont want to create a NEW user?? I am confused
> 
> ```
> @user = User.new(params[:user])
> username = @user.username
> user = User.find(:first, :conditions => ["username = ?", username])
> 
> ```
> 
> why I cant simply get the form data with  
> username = params[:username]
> 
> and why needs User.find an ID - also when I say "conditions =\>  
> [“username = ?”, username])
> 
> what I am doing wrong?  
> thanks  
> andreas

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [November 3, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/read-params-from-form-ot-working/74285/3 "2006-11-03T14:50:51Z")

</div>

Incidentally, “find\_by” uses the :first functionality - it returns the  
first matching row. If you want are looking for more than one row from  
the database (:all functionality), the dynamic finder is  
“find\_all\_by\_”, like this for instance:

users = User.find\_all\_by\_state(‘FL’)

…will find all users with a “state” value of “FL” on the database.

c.

Cayce B. wrote:

> You \>don’t\< have to do it that way. The params[:user] variable in your  
> controller is a hash of the form data. Use this:
> 
> username = params[:user][‘username’]
> 
> As for what you are trying to do, it is much easier than what you are  
> trying. Rails creates dynamic finders for your models, so if you have a  
> field “username” there is a finder defined as “find\_by\_username”. You  
> can use that with the params value to get your user object in one line:
> 
> user = User.find\_by\_username(params[:user][‘username’])
> 
> Enjoy.  
> c.
> 
> Andreas S. wrote:
> 
> > hi all,
> > 
> > can someone tell me why I need to get the data from a form like the code  
> > below  
> > even when I dont want to create a NEW user?? I am confused
> > 
> > ```
> > @user = User.new(params[:user])
> > username = @user.username
> > user = User.find(:first, :conditions => ["username = ?", username])
> > 
> > ```
> > 
> > why I cant simply get the form data with  
> > username = params[:username]
> > 
> > and why needs User.find an ID - also when I say "conditions =\>  
> > [“username = ?”, username])
> > 
> > what I am doing wrong?  
> > thanks  
> > andreas

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [November 3, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/read-params-from-form-ot-working/74285/4 "2006-11-03T17:08:09Z")

</div>

thanks Cayce!

wooow…thats great - when you come from the MS ASP world - like me -  
you never think about that there are such commands like find\_by\_username  
… I am impressed - really!

I think I have the most problems - because I think in a total different  
way - also when you are regulary type in SQL sequences into asp code to  
get back data from an sql server - its really different to me to work in  
this “new” way. I feel like a kid, doing the first steps in coding. I am  
happy that I know a bit about c and c++ - but dont get the classes in  
RoR. Where do I need to define the classes to access then from  
everywhere. E.g. I want to create a SendEmail class - where do I need to  
define this class to send emails from every controller? These is only  
one question from many in my head.

what I also miss is a documentation which shows e.g. all ways to search  
in a database. find, find\_all, find\_by\_…

anyway- -thanks!  
andreas

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [November 3, 2006, 7:29pm UTC](https://www.ruby-forum.com/t/read-params-from-form-ot-working/74285/5 "2006-11-03T19:29:06Z")

</div>

As for documentation, what you can get for free on the web is generally  
of the reference-style, meaning that you kind of have to know what  
you’re after to go find things. For instance, [http://api.rubyonrails.org](http://api.rubyonrails.org)  
will give you the nitty-gritty of every class, method, etc in the  
system. But, if you don’t know what to look for, you’re prolly out of  
luck.

For more tutorial/“teach me” style documentation, the Pragmatic  
Programmers books are excellent:

Agile Web D. With Rails (commonly referred to as AWDWR)  
[http://www.pragmaticprogrammer.com/titles/rails1/index.html](http://www.pragmaticprogrammer.com/titles/rails1/index.html)

Rails Recipes  
[http://www.pragmaticprogrammer.com/titles/fr\_rr/index.html](http://www.pragmaticprogrammer.com/titles/fr_rr/index.html)

Another excellent resource, specific to using AJAX in Rails:

> **[RJS Templates for Rails](https://www.oreilly.com/library/view/rjs-templates-for/0596528094/)**
>
> RJS templates are an exciting and powerful new type of template added to Rails 1.1. Unlike conventional Rails templates that generate HTML or XML, RJS templates generate JavaScript code that … - Selection from RJS Templates for Rails \[Book\]

You’re looking at about $50 for these, in my mind they’re worth many,  
many multiples of that. If you get the PDF’s you’ll have them almost  
immediately, and you get free updates.

As for email - there is no need to write your own class. ActionMailer is  
part of Rails and will do just about anything you need it to, re: email.  
See: [http://wiki.rubyonrails.org/rails/pages/ActionMailer](http://wiki.rubyonrails.org/rails/pages/ActionMailer) for beginning  
details.

With regards to “getting” the classes in rails, that’s a combination of  
both Ruby and Ruby on Rails. If you want to deep-dive the Ruby language  
itself, then the Programming Ruby book is what you want:

> **[Programming Ruby (2nd edition)](https://pragprog.com/titles/ruby/index.html)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

But that might be overkill - coming from ASP world. There is an appendix  
in AWDWR that will intro you to Ruby.

c.

Andreas S. wrote:

> thanks Cayce!
> 
> wooow…thats great - when you come from the MS ASP world - like me -  
> you never think about that there are such commands like find\_by\_username  
> … I am impressed - really!
> 
> I think I have the most problems - because I think in a total different  
> way - also when you are regulary type in SQL sequences into asp code to  
> get back data from an sql server - its really different to me to work in  
> this “new” way. I feel like a kid, doing the first steps in coding. I am  
> happy that I know a bit about c and c++ - but dont get the classes in  
> RoR. Where do I need to define the classes to access then from  
> everywhere. E.g. I want to create a SendEmail class - where do I need to  
> define this class to send emails from every controller? These is only  
> one question from many in my head.
> 
> what I also miss is a documentation which shows e.g. all ways to search  
> in a database. find, find\_all, find\_by\_…
> 
> anyway- -thanks!  
> andreas

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [November 3, 2006, 7:59pm UTC](https://www.ruby-forum.com/t/read-params-from-form-ot-working/74285/6 "2006-11-03T19:59:55Z")

</div>

thanks for all the hints cayce!

andreas

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [November 7, 2006, 10:58am UTC](https://www.ruby-forum.com/t/read-params-from-form-ot-working/74285/7 "2006-11-07T10:58:11Z")

</div>

params[:user] isnt the username, its a hash which contains all form  
parameters like username and all the other stuff from the form

try this:

username = params[:user][:username]
