我有2個conotrollers和3種型號:未定義的方法`[]」爲零:NilClass誤差在導軌
型號:
problem.rb
class Problem < ActiveRecord::Base
has_many :problemtags
has_many :tags, :through => :problemtags
end
tag.rb
class Tag < ActiveRecord::Base
validate :name, :presence => true
has_many :problemtags
has_many :problems, :through => :problemtags
end
problemtag.rb
class Problemtag < ActiveRecord::Base
belongs_to :problem
belongs_to :tag
end
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 is generate with scaffold
而且我有以下問題中的代碼視圖:
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 %>
時我運行這個項目,th E題的觀點是表演,但是當我完成了文本框,然後選擇標籤,然後點擊提交按鈕,我得到以下錯誤:
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
我不明白的問題。任何人都可以向我描述問題?
哪一行是否22.檢查參數[:標籤]是否有值...什麼是服務器日誌中顯示的參數.plz post – Bijendra
@GhostRider我檢查服務器日誌,我在問題的頁面中選擇標籤,但在服務器日誌,params [:tags]沒有任何價值。 – mgh
如果參數[:標籤]沒有任何然後params [:標籤] [:id]將作爲[] [:id]不能工作會發生錯誤..檢查從視圖返回的參數是什麼,並相應地改變操作代碼 – Bijendra