未定义的方法`[]'为nil:NilClass Ruby对象在lib Rails中 [英] undefined method `[]' for nil:NilClass Ruby Object in lib Rails

查看:204
本文介绍了未定义的方法`[]'为nil:NilClass Ruby对象在lib Rails中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当复杂的表单的应用程序,它在lib文件夹中使用Ruby对象(我第一次使用ruby对象)。我无法弄清楚如何在我的更新嵌套属性窗体中创建multiple:task_ids,尽管这些参数是在我的Ruby对象自定义类中定义的(这是我第一次使用这个,我正在学习如何调用它)。我一直得到这个错误:

I have an app with a fairly complicated form that uses Ruby Objects in the lib folder (my first time with ruby objects). I can't figure out how to create the multiple :task_ids in my Update Nested Attributes form, even though the params are defined in my Ruby Object Custom Class (this is my first time using this and I am learning how to call it). I keep getting this error:

undefined method `[]' for nil:NilClass

我的表单试图做的是创建初始任务&基于预先制定的模板任务和里程碑的入门级里程碑。所以当用户创建一个项目时,他们已经添加了一些常见的任务。这个代码直接涉及这个前面的问题&答案:
通过复选框添加多个嵌套属性Rails 4(也许有多种形式)

What my form is trying to do is create starter tasks & starter milestones based on premade "template" tasks and milestones. So when a user creates a Project, they have some common tasks already added. This code relates directly to this previous question & answer: Add Multiple Nested Attributes through checkboxes Rails 4 (maybe with multiple forms)

我的控制器:

class ProjectsController < ApplicationController

def new_milestones
    @project.milestones.build
    @project.tasks.build
    @milestones_templates = MilestoneTemplate.where(template_id: @project.template_id)
  end

 def update
    respond_to do |format|
      result = ProjectUpdater.perform(@project, update_params) == true
      if result == true
        format.html { redirect_to @project, notice: 'Project was successfully updated.' }
        format.json { render :show, status: :ok, location: @project }
      else
        @project = result
        format.html { render :edit }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  def project_params
      params.require(:project).permit(:id, :name, :template_id, milestone_attributes:[{:names => []},  {:ids => []}, { :milestone_ids => []},:id, :name, :project_id, :milestone_template_id, :project_id, task_attributes: [{:names => []},  {:ids => []}, { :task_ids => []}, :id, :name, :milestone_id, :task_template_id, :project_id, :_destroy]])
    end
  end

lib / project_updater.rb

lib/project_updater.rb

class ProjectUpdater

  def self.perform(project, params)
    **milestones = params[:project][:milestones]** <--- this is the line where the error is
    #Create and save each milestone
    # You might be able to us nested attributes to save tasks.

    if project.update_attributes(params[:project])
      return true
    else
      return project
    end
  end
end

我的表格

<%= form_for @project do |f| %>
  <% @milestones_templates.each_with_index do |milestone, index| %>
    <br>
    <%= f.fields_for :milestones, index: index do |fm| %>
      <%= fm.hidden_field :name, value: milestone.name %>
      <!-- Create a checkbox to add the milestone_id to the project -->
      <%= fm.label milestone.name %>
      <%= fm.check_box :milestone_template_id,{}, milestone.id %>
      <br>
      <% milestone.task_templates.each_with_index do |task, another_index| %>
        <%= fm.fields_for :tasks, index: another_index do |ft| %>
          <!-- Create a checkbox for each task in the milestone -->
          <%= ft.label task.name %>
          <%= ft.check_box :task_ids, {}, task.id %>
        <% end %>
      <% end %>
      <br>
    <% end %>
  <% end %>
  <br>
<%= f.submit %>
  <% end %>

提交内容:

what is being submitted:

{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"do/NeMW4wvsh8PCOIYAvuDiH0DwKVr6v0Sm55gOuCHM/Fw5h9T0orRjrspcQpNSx5Vp2JOmtVf+3O18P2XB4vA==", "project"=>{"milestones"=>{"0"=>{"name"=>"Yellow Milestone 1", "milestone_template_id"=>"18", "tasks"=>{"0"=>{"task_ids"=>"1"}, "1"=>{"task_ids"=>"0"}, "2"=>{"task_ids"=>"0"}}}, "1"=>{"name"=>"Yellow Milestone 2", "milestone_template_id"=>"0", "tasks"=>{"0"=>{"task_ids"=>"0"}, "1"=>{"task_ids"=>"0"}, "2"=>{"task_ids"=>"0"}}}, "2"=>{"name"=>"Yellow Milestone 3", "milestone_template_id"=>"0", "tasks"=>{"0"=>{"task_ids"=>"0"}, "1"=>{"task_ids"=>"0"}, "2"=>{"task_ids"=>"0"}}}}}, "commit"=>"Update Project", "controller"=>"projects", "action"=>"update", "id"=>"155"}

config / application.rb

config/application.rb

require File.expand_path('../boot', __FILE__)

require 'rails/all'

Bundler.require(*Rails.groups)


module Taskit


    class Application < Rails::Application

        config.autoload_paths += %W(#{config.root}/lib)
        config.active_record.raise_in_transactional_callbacks = true
      end


    end


推荐答案

首先我认为这行有一个错字:

First of all I think there is a typo in this line:

result = ProjectUpdater.perform(@project, update_params)

我没有看到 update_params 方法,所以我认为这是应该是 project_params

I don't see an update_params method, so I think this should be project_params instead.

ProjectUpdater中的问题是因为 params.require(:项目)返回一个没有outer:项目键的散列,所以在 ProjectUpdater.perform 方法中删除:project键:

The problem in ProjectUpdater is because params.require(:project) returns a hash without the outer :project key, so in the ProjectUpdater.perform method get rid of the :project key:

milestones = params[:milestones]

这篇关于未定义的方法`[]'为nil:NilClass Ruby对象在lib Rails中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆