【发布时间】:2014-08-06 04:25:32
【问题描述】:
当我尝试从嵌套表单创建新记录时收到此消息。奇怪的是,当我退格到上一页(带有“创建”按钮的页面)并再次点击“创建”时,记录就被创建了。所以我不确定为什么它不会第一次创建记录,因为在第一次和第二次按下按钮期间我没有更改控制器中的创建方法。在此之前经历过的任何人都可以帮助我理解为什么会发生这种情况会很棒。
型号
class Benefit < ActiveRecord::Base
belongs_to :account
has_many :employee_benefits
has_many :benefit_plans, :inverse_of => :benefit
belongs_to :benefit_coverage_period
belongs_to :benefit_type_id, :class_name => "LookupTable", :foreign_key => "benefit_type"
attr_accessible :account_id, :active, :attachment, :automatic_rollover, :id, :benefit_id, :benefit_type_id
attr_accessible :benefit_coverage_id, :benefit_type, :is_pretax, :benefit_coverage_period_id
attr_accessible :description, :enrollable, :link, :name, :has_plans, :has_custom_amount, :benefit_plans_attributes
validates_format_of :link, :message => "Please enter a valid URL", :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
accepts_nested_attributes_for :benefit_plans, allow_destroy: true
end
class BenefitPlan < ActiveRecord::Base
belongs_to :benefit, :inverse_of => :benefit_plans
validates_presence_of :benefit
has_many :employee_benefits
has_many :benefit_coverages
attr_accessible :benefit_id, :description, :name, :benefit_coverages_attributes, :link, :attachment
accepts_nested_attributes_for :benefit_coverages, allow_destroy: true
end
class BenefitCoverage < ActiveRecord::Base
belongs_to :benefit_plan
has_many :employee_benefits
belongs_to :name_id, :class_name => "LookupTable", :foreign_key => "name"
attr_accessible :benefit_plan_id, :name, :paycheck_deduction, :percentage_split, :total_cost
accepts_nested_attributes_for :employee_benefits
end
控制器
def new
@benefit = Benefit.new
@benefit_coverage_periods = @account.benefit_coverage_periods
1.times { @benefit.benefit_plans.build(:name => 'Default Plan') }
@lookup_tables = LookupTable.where(:active => :true).find_all_by_group(30) || []
@lookup_tables1 = LookupTable.where(:active => :true).find_all_by_group(31) || []
end
def create
@benefit = Benefit.new(params[:benefit])
@lookup_tables = LookupTable.where(:active => :true).find_all_by_group(30) || []
@lookup_tables1 = LookupTable.where(:active => :true).find_all_by_group(31) || []
@benefit_coverage_periods = @account.benefit_coverage_periods
if @benefit.save
redirect_to benefits_path, :notice => 'Benefit was successfully created.'
else
render :action => "new"
end
查看
= form_for @benefit, :html => { :class => "form-horizontal" }, :validate => true do |f|
= f.hidden_field :account_id, :value => @account.id
.control-group
= f.label :name, :class => "control-label"
.controls
= f.text_field :name, :class => "text_field"
.control-group
= f.label :benefit_type, "Benefit Type", :class => "control-label"
.controls
= f.collection_select :benefit_type, @lookup_tables1, :id, :title, :prompt => true
...
错误
NoMethodError - undefined method `map' for nil:NilClass:
15:00:09 web.1 | actionpack (3.2.14) lib/action_view/helpers/form_options_helper.rb:364:in `options_from_collection_for_select'
15:00:09 web.1 | actionpack (3.2.14) lib/action_view/helpers/form_options_helper.rb:600:in `to_collection_select_tag'
15:00:09 web.1 | actionpack (3.2.14) lib/action_view/helpers/form_options_helper.rb:191:in `collection_select'
15:00:09 web.1 | actionpack (3.2.14) lib/action_view/helpers/form_options_helper.rb:646:in `collection_select'
15:00:09 web.1 | client_side_validations (3.2.6) lib/client_side_validations/action_view/form_builder.rb:77:in `collection
_select_with_client_side_validations'
15:00:09 web.1 | app/views/benefits/_form.html.haml:10:in `block in _app_views_benefits__form_html_haml___5210787370672092
58_70323180464100'
15:00:09 web.1 | haml (4.1.0.beta.1) lib/haml/helpers/action_view_mods.rb:132:in `block (2 levels) in form_for_with_haml'
15:00:09 web.1 | haml (4.1.0.beta.1) lib/haml/helpers.rb:284:in `with_tabs'
15:00:09 web.1 | haml (4.1.0.beta.1) lib/haml/helpers/action_view_mods.rb:132:in `block in form_for_with_haml'
15:00:09 web.1 | actionpack (3.2.14) lib/action_view/helpers/capture_helper.rb:40:in `block in capture'
15:00:09 web.1 | actionpack (3.2.14) lib/action_view/helpers/capture_helper.rb:187:in `with_output_buffer'
...
【问题讨论】:
标签: ruby-on-rails-3