# Model objects, link\_to and view

**URL:** <https://www.ruby-forum.com/t/model-objects-link-to-and-view/158111>\
**Category:** Rails\
**Created:** [January 19, 2009, 1:48pm UTC](https://www.ruby-forum.com/t/model-objects-link-to-and-view/158111 "2009-01-19T13:48:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [January 19, 2009, 1:48pm UTC](https://www.ruby-forum.com/t/model-objects-link-to-and-view/158111/1 "2009-01-19T13:48:13Z")

</div>

Hi, this is probably a very easy problem to solve but ive googled and  
searched this site no end and cant find a suitable solution - to be fair  
im not really sure of the appropriate search terms.

Im praticing ruby by making a simple todo list. I have two tables  
projects and tasks. In my main “index” view Ill have two distinct areas.  
The left side will list all projects titles vertically with one in  
‘focus’. In the right area i want to list all tasks associated with that  
in “focus” project. If a user clicks on a different project on the left  
the tasks are updated.

How do I go about creating these project links wihtout creating  
individual actions for each project title.

Im wondering if i should be using link\_to for this in my view, should i  
link it back to my index view perhaps passing my params[:project\_id] via  
the link\_to ??

\id also need to have soem code in my controller

def index  
@all\_projects = Project.find(:all)  
#coming from somewhere else than the index view so choose a project to  
be in focus initially  
if Coming\_from\_somewhere\_else  
@in\_focus\_project = all\_projects.first  
@in\_focus\_asscociated\_tasks = get\_project\_tasks(@in\_focus\_project.id)  
#user clicked on a project link from within the view itself  
else  
@in\_focus\_project = Project.find(params[:id])  
@in\_focus\_asscociated\_tasks = get\_project\_tasks(@in\_focus\_project.id)  
end  
end

and then have the same index view loop through  
@in\_focus\_asscociated\_tasks to list the tasks.

is this the right way of going round this?

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [January 19, 2009, 1:48pm UTC](https://www.ruby-forum.com/t/model-objects-link-to-and-view/158111/2 "2009-01-19T13:48:59Z")

</div>

rather

> Im praticing RAILS

😉

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [January 19, 2009, 3:22pm UTC](https://www.ruby-forum.com/t/model-objects-link-to-and-view/158111/3 "2009-01-19T15:22:50Z")

</div>

Starting off, I’d go with the simplest thing that could possibly work…

In the controller, get all your projects to populate the left pane.  
If you received a focus\_project\_id in the params to the index method in  
the contorller, retrieve the tasks for that project, otherwise retrieve  
the tasks for the first project.

In the index view, iterate the left pane across the projects with  
something like

@all\_projects.each do |project|  
output your project stuff  
end

then iterate the right pane contents with an

@in\_focus\_associated\_tasks.each do |task|  
output your task stuff  
end

So you’ve got the basic notion well in hand it seems. Once you’re more  
comfortable with Rails, you could look into using an Ajax request to  
update just the tasks when a different project is selected, but defer  
that stuff 'till later.

Given that you’ll probably be displaying the tasks for a project  
somewhere else in your app, it’s an ideal place to insert a partial for  
the task listing, but get the basics up and running, then extend.

Kind of like building a house, it’s much easier if you have the  
foundation built before adding the first, or second floor…
