Ask A Question

Notifications

You’re not receiving notifications from this thread.

link_to question

Adrian DeGus asked in Rails

I'm working on a project management app that has a dashboard which links to tasks that are overdue, due today, this week, next week etc.

I can display the task titles fine but can't get the links to work. I also want to display the associated project each linked task belongs to, but can't get that to work either.

The problem seems to be my inability to call the associated project ID for each task.

Controller:

def index
    @project = Project.new
    @projects = Project.all
    @tasks = @project.tasks.all 
    # Custom Scopes
    @tasks_overdue = Task.overdue
    @tasks_today = Task.today
end

View:

<% @tasks_overdue.each do |task| %>
      <%= link_to task.title, project_task_path(@project, task) %>
      <br>
      <%= @project.project_title %>
<% end %>

Since this page is on today#index I can't pull an ID from the address bar. Normally something like @tasks = @project.tasks.all works fine, just not in this case.

Is there any way I can pull the ID for the project associated with each task, since I can iterate through those without issue?

Reply

My first thought is that your associations are not set correctly (ie. Project, has_many :tasks / Task, belongs_to :project). From there you should be able to set, like @tasks.first.project.id or in #show @task.project.id.

Unrelated to the question, but: it might be useful for you to put your scope(s) into the model, along the lines of: scope :due_today, -> { where(due_date: DateTime.now) }. You can use these scopes then like: @project.tasks.due_today.

Reply

task.project.id and task.project.project_title did the trick Jack, thanks!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.