Hi Micheal,
On 11/29/05, Michael T. [email protected] wrote:
I have a controller named Login, a model named Login and a table named
logins (why did Rails insist that this had to be logins, instead of
login?) and the controller looks like this:
Plural table names are a convention in Rails. Rails is convention based
and
if you follow the convention it makes life easier. Not that you have to
though. Look at the docs if you want to call your table / primary key
something different.
class LoginController < ApplicationController
end
email varchar(96) default "",
real_name varchar (64) default ""
);
Now what I can’t figure out right now is how do I connect to the
database the “rails way,” and check to see if formVars[“username”] and
formVars[“password”] are valid according to the entry in the database?
In general, in your view, you would call the text fields like
<%= text_field ‘login’, ‘username’ %>
<%= password_field ‘login’, password’ %>
This makes @params[:login][‘username’] and @params[:login][‘password’]
available to your controller.
The general format for the text_field is <%= text_field ‘object’,
‘method’
%> If you follow this convention rails will automagically fill out the
form
if it’s not filled out correctly. It will also display and highlight
errors
with the scaffolding.
To confirm that the password / username is valid, you could use in your
controller
@login = Login.new(@params[:user])
if @login.valid?
or
if @login.save
These will tell you if your model is valid acording to your validate_xxx
statements in your model.
ie.
class Login < ActiveRecord::Base
validate_presence_of :username, :password
validate_uniquness_of :username
validate_format_of :email, some regex
etc etc
end
There is pretty good docs on rails wrt this, and there is also plenty of
discussion on this list.
Also, would anyone recommend the book Agile Web D. with Ruby
on Rails as a comprehensive guide to all things Rails?
Definitely I would recommend this book. But you should also supplement
this
with some research into Ruby.
http://documentation.rubyonrails.com/
Whys poignant guide http://poignantguide.net/ruby/ - This is a strange
but
informative book
http://www.ruby-lang.org/en/20020103.html
Gives a good list of ruby related docs to start with.
Good luck
Mike