简体   繁体   English

导轨中nil:NilClass错误的未定义方法“ []”

[英]undefined method `[]' for nil:NilClass error in rails

I have 2 conotrollers and 3 models: 我有2个控制器和3个模型:

Models: 楷模:

problem.rb problem.rb

class Problem < ActiveRecord::Base
  has_many :problemtags
  has_many :tags, :through => :problemtags
end

tag.rb tag.rb

class Tag < ActiveRecord::Base
  validate :name, :presence => true
  has_many :problemtags
  has_many :problems, :through => :problemtags
end

problemtag.rb problemtag.rb

class Problemtag < ActiveRecord::Base
  belongs_to :problem
  belongs_to :tag
end

problems_controller.rb problems_controller.rb

class ProblemsController < ApplicationController
def new
  @all_tags = Tag.all
  @new_problem = @problem.problemtags.build
end
def create
  params[:tags][:id].each do |tag|
    if !tag.empty?
      @problem.problemtags.build(:tag_id => tag)
    end
  end
end
def problem_params
  params.require(:problem).permit(:reporter_id, :status, :date_time, :trace_code)
end

tags_controller.rb tags_controller.rb

//tags_controller is generate with scaffold

And I have below code in problems view: 我在问题视图中有以下代码:

new.html.erb new.html.erb

<%= fields_for(@new_problem) do |f| %>
    <div class="field">
      <%= f.label "All Tags" %><br>
      <%= collection_select(:tags, :id, @all_tags, :id, {}, {:multiple => true}) %>
    </div>
<% end %>

when I run the project, the problem's view is show, but when I complete the textfields and select tags and then click on submit button, I get below error: 当我运行项目时,显示问题的视图,但是当我完成文本字段并选择标签,然后单击提交按钮时,出现以下错误:

NoMethodError in ProblemsController#create
undefined method `[]' for nil:NilClass

Extracted source (around line #22):   
  @problem = @reporter.problems.build(problem_params)

  params[:tags][:id].each do |tag|
    if !tag.empty?
      @problem.problemtags.build(:tag_id => tag)
    end

I do not understand the problem. 我不明白这个问题。 any one can describe the problem to me? 有人可以向我描述这个问题吗?

As stated by your answers, your issue is that you're not sending the right data to your controller (and consequently params[:tags] will be blank): 如您的回答所述,您的问题是您没有向控制器发送正确的数据(因此params[:tags]将为空白):

Form 形成

You're firstly missing the form_builder object in your collection_select (so your tags will likely not be sent inside the correct params hash). 首先,您会在collection_select缺少form_builder对象(因此,您的标签可能不会在正确的params哈希中发送)。 Although this may be by design, you need to ensure you're passing the data properly: 尽管这可能是设计使然,但您需要确保正确传递数据:

<%= fields_for(@new_problem) do |f| %>
    <div class="field">
      <%= f.label "All Tags" %><br>
      <%= f.collection_select(:tags, :id, @all_tags, :id, {}, {:multiple => true}) %>
    </div>
<% end %>

Params PARAMS

Secondly, we cannot see your form or params hash. 其次,我们看不到您的表单或参数哈希。 This is vital, as your form needs to look like this: 这至关重要,因为您的表单需要如下所示:

<%= form_for @variable do |f| %>
    <%= f.text_field :value_1 %>
    <%= f.text_field :value_2 %>
<% end %>

This creates a params hash like this: 这将创建一个参数哈希,如下所示:

params { "variable" => { "name" => "Acme", "phone" => "12345", "address" => { "postcode" => "12345", "city" => "Carrot City" }}}

This will be the core reason why your controller will return the [] for nil:NilClass error - you'll be referencing params which don't exist. 这就是您的控制器将为错误[] for nil:NilClass返回[] for nil:NilClass的核心原因-您将引用不存在的参数。 You'll need to call params[:variable][:tags] as an example 您需要以params[:variable][:tags]为例

If you post back your params hash, it will be a big help 如果您将params散列回发,将会有很大帮助

您可以尝试使用validate :tag_id, :presence => true来检查所需参数的存在。

I found 2 problems in my code: 我在代码中发现了2个问题:

  1. in new.index.html(in problem view), the submit button is in the form_for and I write the field_for outside the form_for and when I click on submit button, the params hash of tags didn't create. 在new.index.html(在问题视图中)中,“提交”按钮位于form_for中,我在form_for外部编写了field_for,当我单击“提交”按钮时,未创建标签的params哈希。

  2. In collection_select, I forgot to add the name parameter of tag. 在collection_select中,我忘记添加标签的名称参数。

Correct new.html.erb code: 正确的new.html.erb代码:

<%= form_for @problem do |f| %>
    status: <%= f.text_field :status %><br/>
    datetime: <%= f.datetime_select :date_time %><br/>
    trace code: <%= f.text_field :trace_code %><br/>

    <%= fields_for(@new_problem) do |f| %>
        <div class="field">
          <%= f.label "All Tags" %><br>
          <%= collection_select(:tags, :id, @all_tags, :id,:name, {}, {:multiple => true}) %>
        </div>
    <% end %>
    <%= f.submit %>
<% end %>

Thanks for all of the answers. 感谢您提供所有答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM