具有三重嵌套的Form_for的不匹配约束错误

问题描述 投票:0回答:2

我正在制作一个事件应用,其中每个事件可以有多个待办事项,每个任务都有各自的任务。

我正在为任务的第三级嵌套而苦苦挣扎(todo_items)

这是我的模特:

Event.rb

has_many :todo_lists, :dependent => :destroy
has_many :todo_items, :through => :todo_lists 

accepts_nested_attributes_for :todo_lists
accepts_nested_attributes_for :todo_items

Todo_list.rb

 has_many :todo_items, :dependent => :destroy

 accepts_nested_attributes_for :todo_items

 belongs_to :event

Todo_item.rb

belongs_to :todo_list

Todo_list_Controller.rb

def set_todo_list
  @todo_list = @event.todo_lists.find(params[:id])
end

def set_event
  @event = Event.find(params[:event_id])
end

def todo_list_params
  params.require(:todo_list).permit(:title, :description, :event_id)
end

Todo_items_Controller.rb

def set_todo_list
  @todo_list = TodoList.find(params[:todo_list_id])
end

def set_todo_item
  @todo_item = @todo_list.todo_items.find(params[:id])
end

def todo_item_params
  params[:todo_item].permit(:content, :todo_list_id)
end

routes.rb

resources :events do
    resources :todo_lists do
      resources :todo_items do
        member do
          patch :complete
        end
      end
    end
end

如果需要任何其他代码,我将进行更新!预先感谢!

我已尽力嵌套对象。我可以将任务(todo_items)添加到todo_list中,但是无法删除或完成它们。

错误来自todo_LIST_controller而不是todo_ITEMS_controller

错误日志:

ActiveRecord::RecordNotFound (Couldn't find TodoList with 'id'=5 [WHERE "todo_lists"."event_id" = ?]):
ruby forms ruby-on-rails-5 nested-forms nested-form-for
2个回答
0
投票

我认为您必须确保路由正确嵌套

从事件到todo_items

resources :event do
  resources :todo_lists do
     resources :todo_items
  end
end

我不知道您如何设置路线,但以上内容只是为了说明我要说的话。希望有帮助


0
投票

这是重定向路径的路由错误。

错误的路线是:

redirect_to event_todo_list_path(@event)

仅缺少@todo_list参数。

redirect_to event_todo_list_path(@event, @todo_list)
© www.soinside.com 2019 - 2024. All rights reserved.