Forum: Ruby on Rails model objects, link_to and view

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
803ad415efcdcbf09b1addeb319650d7?d=identicon&s=25 Adam Akhtar (thegeezer3)
on 2009-01-19 13:48
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(
#user clicked on a project link from within the view itself
   @in_focus_project = Project.find(params[:id])
   @in_focus_asscociated_tasks = get_project_tasks(

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?
803ad415efcdcbf09b1addeb319650d7?d=identicon&s=25 Adam Akhtar (thegeezer3)
on 2009-01-19 13:48

> Im praticing RAILS

D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2009-01-19 15:22
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

then iterate the right pane contents with an

@in_focus_associated_tasks.each do |task|
  output your task stuff

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...
This topic is locked and can not be replied to.