【发布时间】:2014-06-26 05:44:09
【问题描述】:
我是红宝石新手。我需要将数组文本框值插入到 has_many 和 belongs_to 关系中。我使用了两个模型 intrrattes 和 intrsetups。
这是我的 new.html.erb 文件
<%= form_for @intrsetup do |f| %>
<div class='row'>
<div class='span6'>
<div class="control-group">
<label class=" control-label">Effective From<abbr title="required">*</abbr></label>
<div class="controls">
<%= f.text_field :effective_from, :onclick => "return calender()" %>
</div>
</div>
</div>
<div class='span6'>
<div class="control-group">
<label class=" control-label">Effective To</label>
<div class="controls">
<%= f.text_field :effective_to %>
</div>
</div>
</div>
</div>
<%= f.fields_for :intrrates do |builder| %>
<h3>Interest Rates</h3>
<table class='table condensed-table'>
<tr>
<td>
Days From
</td>
<td>
Days To
</td>
<td>
Rate
</td>
<td>
Senior Increment
</td>
<td>
Super Senior Increment
</td>
<td>
Widow Increment
</td>
</tr>
<tr>
<td>
<%(1..2).each do |i|%>
<%= builder.text_field(:days_from, :name => "intrrate[days_from][]", :id => "intrrate_days_from_#{i}") %>
<%end%>
这是我的 Intrrate 和 Intrsetup 模型代码
class Intrrate < ActiveRecord::Base
belongs_to :intrsetup
#attr_accessor :effective_from, :effective_to
attr_accessible :effective_from, :effective_to
attr_accessible :days_from, :days_to, :rate, :senior_increment, :super_senior_increment, :widow_increment, :intrsetup_id
end
class Intrsetup < ActiveRecord::Base
has_many :intrrates
accepts_nested_attributes_for :intrrates
attr_accessible :intrrates_id, :effective_from, :effective_to, :intrrates_attributes
end
这是我的控制器页面
class IntrsetupsController < ApplicationController
def new
@intrsetup = Intrsetup.new
@intrrate = @intrsetup.intrrates.build
end
def create
@intrsetup = Intrsetup.new(params["intrsetup"])
@intrsetup.save
end
end
class IntrratesController < ApplicationController
def index
@intrrate = Intrrate.all
end
def new
@intrrate = Intrrate.new
end
def create
puts @intrrate = Intrrate.new(params["intrrate"])
@intrrate.save
end
end
我的 schema.rb
create_table "intrrates", :force => true do |t|
t.integer "days_from"
t.integer "days_to"
t.float "rate"
t.float "senior_increment"
t.float "super_senior_increment"
t.float "widow_increment"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "intrsetup_id"
t.integer "deposit_id"
end
create_table "intrsetups", :force => true do |t|
t.date "effective_from"
t.date "effective_to"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
我的错误页面 IntrsetupsController#create 中的 NoMethodError
undefined method `[]' for nil:NilClass
Rails.root: /home/tbf/rails_projects/ccddeposit
Application Trace | Framework Trace | Full Trace
app/controllers/intrsetups_controller.rb:9:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"WsfTU31o9LLfcoieNL3pgpRRu/swqreaXDdo6LxrdsM=",
"intrsetup"=>{"effective_from"=>"1994/12/06",
"effective_to"=>"1994/12/06"},
"intrrate_days_from_1"=>"1",
"intrrate_days_to_1"=>"45",
"intrrate_rate_1"=>"0.5",
"intrrate_senior_increment_1"=>"0.5",
"intrrate_super_senior_increment_1"=>"0.56",
"intrrate_widow_increment_1"=>"0.5",
"intrrate_days_from_2"=>"45",
"intrrate_days_to_2"=>"95",
"intrrate_rate_2"=>"0.5",
"intrrate_senior_increment_2"=>"0.7",
"intrrate_super_senior_increment_2"=>"0.8",
"intrrate_widow_increment_2"=>"0.5",
"commit"=>"Create Intrsetup"}
but i'm getting the following error
如何解决这个错误?
【问题讨论】:
-
显示你的
method的一些代码 -
@nithin: 我已经编辑了我的代码检查它。
-
您在哪一行收到该错误?
-
@Pavan: 我猜这行 "intrrate[ days_from][]", :id => "intrrate_days_from_#{i}") %>
-
@Pavan:我收到以下错误行。IntrsetupsController 中的 NoMethodError#create undefined method `to_f' for ["5.2"]:Array
标签: ruby-on-rails arrays ruby-on-rails-3