簡體   English   中英

嵌套路線-Ruby on Rails

[英]Nested Routes - Ruby on Rails

我正在嘗試獲取諸如“ project /:project_id / task /:task_id”之類的嵌套路由,但是我沒有任何運氣。 我已經這樣設置了路由文件:

    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

但是我在projects.show.html中的鏈接不斷出現錯誤,該鏈接的設置如下:

<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 %>

我知道我應該在“ <%= link_to task.notes%>”之后添加路徑,並且我運行過rake:routes並嘗試了我的許多選項,但是它們都產生URI錯誤。

我不確定這是我在路線中寫資源的方式還是什么。 有嵌套路線專業知識的人(在Ruby on Rails中)可以幫助我嗎? 我在其中一個在線編碼訓練營中,甚至講師也有些困惑。

您是否可以嘗試將此get 'projects/:id/tasks/:id', to: 'tasks#show'get 'projects/:id/tasks/:id', to: 'tasks#show, as: 'project_task'

在您看來<%= link_to task.notes, project_task_path(@project,task) %>希望對您<%= link_to task.notes, project_task_path(@project,task) %>幫助。

你會這樣做

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

該數組將用於根據數組中各項的型號名稱和ID生成一個url(有關更多信息,請查看多態路由)

projects/:project_id/tasks/:id

順便說一句,您還需要表單

form_for [@project, @task] ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM