更新嵌套元素时缺少必需的键:[:id]

时间:2017-01-03 19:36:21

标签: ruby-on-rails ruby-on-rails-5

首先非常感谢帮助我。

我正在研究一个项目工具,我有一个项目模型has_many project_tasks。 project_tasks属于一个项目。

我正在尝试将标记作为项目任务的完整操作,但我似乎无法在补丁操作中获得project_task id。

这是我的路线: (请注意,我对项目也有完整的操作,但这样做很好)

resources :projects do
    resources :project_tasks do
      member do
        patch :complete
      end
    end
    member do
      patch :complete
    end
  end

耙路线

complete_project_project_task PATCH  /projects/:project_id/project_tasks/:id/complete(.:format) project_tasks#complete
        project_project_tasks GET    /projects/:project_id/project_tasks(.:format)              project_tasks#index
                              POST   /projects/:project_id/project_tasks(.:format)              project_tasks#create
     new_project_project_task GET    /projects/:project_id/project_tasks/new(.:format)          project_tasks#new
    edit_project_project_task GET    /projects/:project_id/project_tasks/:id/edit(.:format)     project_tasks#edit
         project_project_task GET    /projects/:project_id/project_tasks/:id(.:format)          project_tasks#show
                              PATCH  /projects/:project_id/project_tasks/:id(.:format)          project_tasks#update
                              PUT    /projects/:project_id/project_tasks/:id(.:format)          project_tasks#update
                              DELETE /projects/:project_id/project_tasks/:id(.:format)          project_tasks#destroy
             complete_project PATCH  /projects/:id/complete(.:format)                           projects#complete
                     projects GET    /projects(.:format)                                        projects#index
                              POST   /projects(.:format)                                        projects#create
                  new_project GET    /projects/new(.:format)                                    projects#new
                 edit_project GET    /projects/:id/edit(.:format)                               projects#edit
                      project GET    /projects/:id(.:format)                                    projects#show
                              PATCH  /projects/:id(.:format)                                    projects#update
                              PUT    /projects/:id(.:format)                                    projects#update
                              DELETE /projects/:id(.:format)                                    projects#destroy

观点:

<%= link_to '', complete_project_project_task_path(@project, project_task.id), class: "glyphicon glyphicon-ok", id: "complete-#{project_task.id}", method: :patch %>

控制器动作:

def complete
    @project = Project.find(params[:project_id])
    @project_task = project.project_task.find(params[:id])
    if @project_task.completed_at.blank?
      @project_task.update_attribute(:completed_at, Time.now)
      flash[:success] = 'Task Completed!'
      redirect_to root_path
    else
      @project_task.update_attribute(:completed_at, '')
      flash[:info] = 'Task Updated!'
      redirect_to root_path
    end
  end

我得到的错误是: 没有路由匹配{:action =&gt;“complete”,:controller =&gt;“project_tasks”,:id =&gt; nil,:project_id =&gt;“26”}缺少必需的键:[:id]

在link_to中我添加了2个参数,@ project(获取项目ID)和project_task.id。 看来我得到的项目ID是正确的,但不是project_task.id。 我试图将控制器操作更改为@project_task = ProjectTask.find(params [:id]),但没有任何运气。 (我也在link_to中用不同的参数尝试了一些事情) 我不知道如何检索,但我希望你们中的一个能指出我正确的方向。

提前致谢!

2 个答案:

答案 0 :(得分:0)

看起来您的project_task变量未正确设置。你的代码应该是这样的:

<% @project.project_tasks.each do |project_task| %>
  <%= link_to '', complete_project_project_task_path(@project, project_task.id), class: "glyphicon glyphicon-ok", id: "complete-#{project_task.id}", method: :patch %>
<% end %>

(这会考虑您的Project型号has_many :project_tasks)。

如果您正确设置,是否可以提供完整视图及其控制器操作?

答案 1 :(得分:0)

好的,我弄清楚问题是什么 我有2个版本的@project_task实例,它创建了冲突。 (来自形式和完整行动) 所以我添加了

<% @project.project_tasks.select(&:persisted?).each do |project_task| %>

修复是添加&amp ;: persisted?为了只显示保存的对象。