user2951911
user2951911

Reputation: 5

Linking id to person (ruby on rails)

I have a list of worker.id, worker.first_name, and worker.last_name at 0.0.0.0:3000/workers/all.

I need to make the worker.id a link that would direct to the worker's more detailed info, 0.0.0.0:3000/workers/show?id=x (x is the id of the worker).

I already have the latter working using "@worker = db.find_by_id(params[:id])" in the Controller so when I type /workers/show?id=5 I get into that worker's detailed info.

The code in View:

<li><%= link_to(worker.id) %>, <%= worker.first_name %>, <%= worker.last_name %></li>

... creates the /workers/all list properly: "123, Ted, Testman". I just need to make the /workers/all list of ids(123 in above example) into links that direct to the proper worker's info, /workers/show?id=123 in this case. With the above code the id is linking back into /workers/all.

Upvotes: 0

Views: 41

Answers (3)

sameera207
sameera207

Reputation: 16629

It seems like you are using link_to helper incorrectly,

it should be something like

link_to "#{worker.first_name} #{worker.last_name}", worker

and probably as the explanation you have give in the question, just `scaffolding' the worker should give you most of the things you want 'out of the box' , this is a good read about scaffolding

read more here about link_to

HTH

Upvotes: 0

CDub
CDub

Reputation: 13354

You want to link to the worker_path for that worker, passing id as a parameter:

<li><%= link_to(worker_path(worker, :id => worker.id) %>, <%= worker.first_name %>, <%= worker.last_name %></li>

Upvotes: 0

MrTheWalrus
MrTheWalrus

Reputation: 9700

If worker is an ActiveRecord model, you don't even need to use worker_path:

<%= link_to(worker.id, worker) %>

Upvotes: 1

Related Questions