Noob struggling to learn


#1

I am learning Rails by building a real application for someone.
Ultimately the app may become a much larger thing, however for now i
am concentrating on just a tricky login situation. I will essentially
have 2 pages, a ‘start’ and a ‘login’ page. ‘start’ being the initial
screen for the application. What I need this start page to do is pull
the UserID from the URL (assuming the request has the userID) and then
use that UserID to look in a table for another bit of information
(planID type of thing) and store that ‘planID’ in a session to be
brought forward to the login and beyond. Essentially the login can be
very different depending on that planID, this is the reason for this
work on the start page. The start page may also contain some nice
graphics and a disclaimer or something. I used RAKE MIGRATE to build
my table, I created my model for that table and I created a controller
called ‘start’. Everything seems hooked up properly and I can run
it. I created the ‘start’ controller assuming this logic of grabbing
the ID from the URL, looking in the table (SQL?), and storing the
result ID in the the session would go in the controller? If anyone
could take the time to help me over this block, I would appreciate it
very much. I am not even sure what the ruby code would look like for
searing the table, or the ruby code for grabbing the ID from the URL
or storing the information in the session.

advanced thank you
JR


#2

I recommend the book: “Agile Web D. with Rails”.

Check it out at Amazon.com:
http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%2Fwww.amazon.com%2Fdp%2F0977616630%2F&tag=gametroll-20&linkCode=ur2&camp=1789&creative=9325

I found that the book is very much better than the available web
resources. I think after you read the book you’ll find what you want to
do pretty easy with rails.

If you want to store stuff in the session then you do something like (in
your controller):

session[:plan_id] = params[‘plan_id’]

It wasn’t clear to me what you mean by pulling the user_id from the URL,
particularly since that is a start screen, I presume the user hasn’t
logged in yet.

The rails book has a whole section on how to do login, authentication
and such. You probably want to use filters as well.

regards,

Richard.

removed_email_address@domain.invalid wrote:

I am learning Rails by building a real application for someone.
Ultimately the app may become a much larger thing, however for now i
am concentrating on just a tricky login situation. I will essentially
have 2 pages, a ‘start’ and a ‘login’ page. ‘start’ being the initial
screen for the application. What I need this start page to do is pull
the UserID from the URL (assuming the request has the userID) and then
use that UserID to look in a table for another bit of information
(planID type of thing) and store that ‘planID’ in a session to be
brought forward to the login and beyond. Essentially the login can be
very different depending on that planID, this is the reason for this
work on the start page. The start page may also contain some nice
graphics and a disclaimer or something. I used RAKE MIGRATE to build
my table, I created my model for that table and I created a controller
called ‘start’. Everything seems hooked up properly and I can run
it. I created the ‘start’ controller assuming this logic of grabbing
the ID from the URL, looking in the table (SQL?), and storing the
result ID in the the session would go in the controller? If anyone
could take the time to help me over this block, I would appreciate it
very much. I am not even sure what the ruby code would look like for
searing the table, or the ruby code for grabbing the ID from the URL
or storing the information in the session.

advanced thank you
JR


#3

Thanks to both of you.
JR


#4

Jason:

I also recommend it. If you need it right away, you can order a pdf
from http://pragmaticprogrammer.com/titles/rails/index.html.

I also attended Dave T.’ and Mike C.'s Pragmatic Studio class
which was excellent.

Here are some tips:

  1. Lookup routes to see how to get the id from a URL. Your url would
    be like http://example.com/controller/action/:id instead of http://
    example.com/controller/action?id=blah. You could then access the id
    from params[:id] in your controller.
  2. To find an object, you don’t have to write SQL right away. Lookup
    dynamic finders to see how User.find_by_id(:id) will return your user
    object. Or, use User.find() if you want more options.

I agree with Richard that you might want to see the book’s section on
logins. It will save you lots of effort. I know, because I wrote my
own login stuff and later found out how easy if could have been.

Keep asking questions and good luck,

-Anthony


#5

ok here is my first pass…

#parse out the userID from th url
#query DB for planID using the userID
#store planID to session to be used going forward

def url
@planid = userid.find(@params[:id])
end

do you think this method does what I am attempting to do based on my
commented lines?

JR

On Apr 12, 9:45 am, “removed_email_address@domain.invalid”


#6

yes, VERY helpful. But I actualy mistated my intentions slightly.
What I am trying to do is:

#parse out the userID from th url
#query the users table for clientID (using the userID)
#query the plans table (using the newly aquired clientID) for planID
#store planID to session to be used going forward

sorry for the confusion. Your help is appreciated. I would assume
the solution goes in the controller as a method. Most likely in the
users controller hooked up the users table?

thanks again

JR


#7

removed_email_address@domain.invalid wrote:

ok here is my first pass…

#parse out the userID from th url
#query DB for planID using the userID
#store planID to session to be used going forward

def url
@planid = userid.find(@params[:id])
end

do you think this method does what I am attempting to do based on my
commented lines?

JR

On Apr 12, 9:45 am, “removed_email_address@domain.invalid”

Does the plans table have a user_id, or does the users table have a
plan_id ?

If the former,

session[:plan_id] = Plan.find_by_user_id(params[:id]).id

If the latter,

session[:plan_id] = User.find(params[:id]).plan.id

In either case, you’re getting a Plan object from the plans table,
either by finding a user, then getting ‘his’ plan, or by looking up a
Plan directly by the user_id column.

Does this help at all ?

Alan


#8

removed_email_address@domain.invalid wrote:

yes, VERY helpful. But I actualy mistated my intentions slightly.
What I am trying to do is:

Excellent.

#parse out the userID from th url
#query the users table for clientID (using the userID)
#query the plans table (using the newly aquired clientID) for planID
#store planID to session to be used going forward

Thats fine. It’s Rinse…repeat.

As long as you have the association set up (has_many, belongs_to etc)
you can just do:

session[:plan_id] = Users.find(params[:id]).client.plan.id

i.e
User belongs_to Client / Client has_many Users
Client has_one Plan / Plan belongs_to Client

sorry for the confusion. Your help is appreciated. I would assume
the solution goes in the controller as a method. Most likely in the
users controller hooked up the users table?

Well, wherever you have implimented the action you’re invoking.

Say you’re hitting /users/show_plan?id=26

you’ll need, in UsersController

def show_plan

get the @plan so we can show it in show_plan.rhtml

@plan = Users.find(params[:id]).client.plan

store the id in the session for later.

session[:plan_id] = @plan.id
end

Does this help ? This whole actions and controller thing is pretty
fundamental to rails, so if it’s not immediately clear what the above
para means, I’d strongly urge to get and read the AWDwR book from the
Pragmatic Programmers. (but feel free to keep asking questions :slight_smile:

Alan