我正在构建一个项目管理应用程序。 我的系统就像-一个项目有很多功能,一个功能有很多任务。 并且route.rb被定义为
resources :projects do
resources :features do
resources :tasks
end
end
第一个级别(即功能项目)对于新功能表单工作正常,但是当我尝试以->
形式实现新任务表单时 <%= form_for([@feature, @feature.tasks.build], class: "form-group row") do |form| %>
<%= form.label :name %>
<%= form.text_field :name, required:true, class: "form-control" %>
<%= form.submit class: "btn btn-primary m-2" %>
<% end %>
现在显示错误为
这是我的模特->
class Task < ApplicationRecord
belongs_to :feature
end
class Feature < ApplicationRecord
belongs_to :project
has_many :tasks
end
任务控制器看起来像->
class TasksController < ApplicationController
before_action :set_feature
def new
@task = @feature.tasks.new
end
def create
@task = @feature.tasks.new(task_params)
if @task.save
redirect_to project_features_path
else
render :new
end
end
def edit
end
def update
if @task.update(task_params)
redirect_to project_feature_tasks_path
else
render :edit
end
end
def complete
@task.update(completed: true)
redirect_to project_feature_tasks_path
end
def destroy
@feature.task.destroy
redirect_to project_feature_tasks_path
end
private
def set_feature
@feature = Feature.find(params[:feature_id])
end
def task_params
params.require(:task).permit(:name,:completed, :project_id,:feature_id)
end
end
我们非常感谢您的帮助-几天来我一直被这个错误困扰。
答案 0 :(得分:1)
如果您尝试运行$ rails routes
,则可以看到当前路由为何使您失败的原因。
Prefix Verb URI Pattern Controller#Action
project_feature_tasks GET /projects/:project_id/features/:feature_id/tasks(.:format) tasks#index
POST /projects/:project_id/features/:feature_id/tasks(.:format) tasks#create
new_project_feature_task GET /projects/:project_id/features/:feature_id/tasks/new(.:format) tasks#new
edit_project_feature_task GET /projects/:project_id/features/:feature_id/tasks/:id/edit(.:format) tasks#edit
project_feature_task GET /projects/:project_id/features/:feature_id/tasks/:id(.:format) tasks#show
PATCH /projects/:project_id/features/:feature_id/tasks/:id(.:format) tasks#update
PUT /projects/:project_id/features/:feature_id/tasks/:id(.:format) tasks#update
DELETE /projects/:project_id/features/:feature_id/tasks/:id(.:format)
您必须致电:
form_for([@project, @feature, @feature.tasks.build], ...) do |form|
一个更好的主意是将路线嵌套。您可以使用shallow
option:
resources :projects do
resources :features, shallow: true do
resources :tasks
end
end
如果您出于某种原因想要保留嵌套功能的成员路线(显示,编辑,更新,销毁),也可以这样做:
resources :projects do
resources :features
end
resources :features, only: [] do
resources :tasks
end