0

I'm trying to get a nested route like "project/:project_id/task/:task_id", but I'm not having any luck. I've set up my routes file like this:

    Rails.application.routes.draw do

  get 'sessions/create'

  devise_for :users, path: '', path_names: { sign_up: 'users/sign_up'}


  get 'teams/sign_up' => 'teams#new', as: 'new_team'
  get 'teams/:id/users/:user_id' => 'teams#users_index'
  get 'teams/:team_id/users/:id/complete' => 'users#show_complete', as: 'user_complete'

  get 'projects/:id/tasks', to: 'projects#tasks_index'
  get 'projects/:id/tasks/:id', to: 'tasks#show'

  get 'teams/:id/projects/new' => 'projects#new', as: 'new_project'
  post 'teams/:id/projects' => 'projects#create', as: 'projects'

  root 'static#index', as: 'root'

  get '/auth/twitter/callback' => 'sessions#create'

 #resources :tasks do 
  #member do
   # get :toggle_status
  #end
#end

  resources :projects do
    resources :tasks, except: [:index], controller: 'projects/tasks'
  end

  resources :users, only: [:show]
  resources :teams, except: [:new]


end

But I keep getting a error with my link in the projects.show.html, which is setup like this:

<h2><%= @project.name %></h2>
        <%= form_for @task do |f| %>
        <%= f.hidden_field :project_id, value: @project.id %>
        <%= f.hidden_field :user_id, value: current_user.id %>
        <%= f.label :notes %>: <%= f.text_field :notes %>
        <%= f.submit "Add Task" %>
    <% end %>

<% if @project.tasks.any? %>
  <table class="table">
    <tr>
      <th>Task</th>
    </tr>
    <% @project.tasks.each do |task| %>
      <tr>
        <td>
          <%= link_to task.notes %>
          <%= task.status %>
        </td>
        <td>
        <% if !task.id.nil? %>

            <%= link_to "Change Status", toggle_status_task_path(task) %>
        <% end %>
        </td>
      </tr>
    <% end %>

  </table>
<% end %>


<%= link_to "Edit Project", edit_project_path(@project) %> | <%= link_to "Delete Project", project_path(@project), method: :delete %>

I know I'm supposed to add a path after "<%= link_to task.notes %>", and I've ran rake:routes and tried numerous options of mine, but they all generate a URI error.

I'm not sure if it's how I've written my resources in the routes or what. Can anyone with nested routes expertise (in Ruby on Rails) please help me out? I'm in one of those online coding bootcamps and even instructors have been a little confused.

James
  • 411
  • 1
  • 4
  • 18

2 Answers2

0

Can you try to change this get 'projects/:id/tasks/:id', to: 'tasks#show' to get 'projects/:id/tasks/:id', to: 'tasks#show, as: 'project_task'.

And in your view <%= link_to task.notes, project_task_path(@project,task) %> Hope it helps.

0

You would do this

<%= link_to task.notes, [@project, task] %>

This array will be used to generate a url based on the model names and ids of the items in the array (check out polymorphic routes for more info)

projects/:project_id/tasks/:id

btw, you'll also need it for the form

form_for [@project, @task] ...
Mark Swardstrom
  • 17,217
  • 6
  • 62
  • 70