how to insert multi textbox array in ruby on rails -


i'm new ruby. need insert array textbox values has_many , belongs_to relationship.i used 2 models intrrattes , intrsetups.

here new.html.erb file

<%= form_for @intrsetup |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 |builder| %>    <h3>interest rates</h3>   <table class='table condensed-table'>   <tr>     <td>     days     </td>     <td>     days     </td>     <td>     rate     </td>     <td>     senior increment     </td>     <td>     super senior increment     </td>     <td>     widow increment     </td>   </tr>   <tr>     <td>     <%(1..2).each |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 intrrate , 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 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 |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 |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 `[]' 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"} i'm getting following error 

how solve error?

as said, problem rate attending float , give array.

so here code force parameter "rate" float value , give average of rates entered in form :

def create   # in case want average value of different rates enter in form   rate_avg = params["intrsetup"]["intrrates_attributes"]["0"]["rate"].inject(0.0) |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 , tell me if works now.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -