簡體   English   中英

如何在rails中的ruby中插入多個文本框數組

[英]how to insert multi textbox array in ruby on rails

我是紅寶石的新手。 我需要將數組文本框值插入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%>

<%= builder.text_field:days_to,多個:true%> <%= builder.text_field:rate,多個:true%> <%= builder.text_field:senior_increment%> <%= builder.text_field:super_senior_increment%> <% = builder.text_field:widow_increment%> <%結束%> <%= f.submit%>

這是我的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

如何解決這個錯誤?

就像我說的,問題在於rate正在浮動,而您給它一個數組。

因此,以下代碼強制您的參數“ rate”為浮點值,並為您提供在表格中輸入的所有費率的平均值:

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

試試這個,告訴我現在是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM