I'm new to ruby. i need insert the array textbox values to has_many and belongs_to relationship.i used two models intrrattes and intrsetups.
here is my new.html.erb file
<%= 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%>
<%= builder.text_field :days_to, multiple: true %> <%= builder.text_field :rate, multiple: true %> <%= builder.text_field :senior_increment %> <%= builder.text_field :super_senior_increment %> <%= builder.text_field :widow_increment %> <% end %> <%= f.submit %>
here is my Intrrate and Intrsetup model code
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
here is my controller page
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
my 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
my error page NoMethodError in IntrsetupsController#create
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
how to solve this error?
As I said, the problem is rate
is attending a float and you give to it an Array.
So here is a code which force your parameter "rate" as a float value and give you the average of all rates entered in your form :
def create
# In case where you want the average value of all different rates you enter in your form
rate_avg = params["intrsetup"]["intrrates_attributes"]["0"]["rate"].inject(0.0) do |value, rate|
value += rate.to_f
end
params["intrsetup"]["intrrates_attributes"]["0"]["rate"] = rate_avg / params["intrsetup"]["intrrates_attributes"]["0"]["rate"].count
@intrsetup = Intrsetup.new(params["intrsetup"])
@intrsetup.save
end
Try this and tell me if it works now.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.