Model objects, link_to and view


#1

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?


#2

rather

Im praticing RAILS

:wink:


#3

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…