Read params from form ot working

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

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

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

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

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
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

Rails Recipes
http://www.pragmaticprogrammer.com/titles/fr_rr/index.html

Another excellent resource, specific to using AJAX in Rails:

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 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:

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

thanks for all the hints cayce!

andreas

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]