【发布时间】:2013-12-05 08:08:07
【问题描述】:
我已经看到了许多类似的问题,并查看了答案,但似乎没有任何帮助我,我已经研究了一段时间。错误是“无法将符号转换为整数”。我的目标是能够为每场比赛创建多个 sub_groups。我只是暂时尝试创建一个。这是相关代码...
** 更新 **
查看
<%= simple_form_for(@race, :url => form_path, :method => form_method, :html => { :class =>
'form-horizontal form-compressed' }) do |f| %>
<fieldset>
<%= f.simple_fields_for :sub_groups do |g| %>
<%= g.input :name, requred: false %>
<%= g.collection_radio_buttons :discount_type,
[['dollars', '$'], ['percent', '%']], :first, :last %>
<%= g.input :discount_amount, :as => :integer, required: false %>
<% end %>
<hr/>
** 赛车模型**
class Race < ActiveRecord::Base
has_many :sub_groups
accepts_nested_attributes_for :sub_groups
attr_accessible :sub_groups_attributes
** SUB_GROUP 模型 **
class SubGroup < ActiveRecord::Base
belongs_to :race
has_many :race_users
attr_accessible :discount_amount, :discount_type, :display_results, :name
end
参数 在我的代码更新之后...
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"VihBL4TDT/Lte4YBji/4fp4XvOri1UgUZ8B33wQuCko=", "race"=>
{"sub_group"=>{"name"=>"dfd", "discount_type"=>"dollars", "discount_amount"=>"2"}},
"commit"=>"Next", "wizard"=>"2", "id"=>"13-test5"}
控制器
class RacesController < ApplicationController
def new
@race = Race.new
@sub_groups = @race.sub_groups.build
@wizard_step = -1
@wizard_step_name = Race.wizard_step_name_from_id @wizard_step
@wizard_mode = true
render :layout => "race_wizard"
end
def update
@race = Race.find params[:id]
@wizard_step = params[:wizard].to_i + 1
@race.wizard_step = @wizard_step
@race.update_attributes(params[:race])
end
所以我从答案 1 中得到建议,并切换到在视图中使用 :sub_groups。现在我有一个新问题,即子组字段根本不显示,尽管我在 #new 方法中构建了一个 sub_groups 东西。 我真的很难过我怎么能做到这一点。这让我发疯。任何帮助深表感谢。谢谢!
【问题讨论】:
-
我们需要更多细节。究竟是什么时候抛出这个错误 - 我假设它是在提交之后。如果是这样,您能否粘贴您的控制器代码?此外,将错误本身与堆栈一起粘贴会有很大帮助。
-
我更新了有关控制器操作的更多详细信息。还根据第一个答案更改了一些内容。是的,问题是在提交后发生的。但是现在我遇到了一个新问题,那就是字段根本不显示。我想当#new 方法被击中时我正在构建一个子组属性,但也许我做错了什么?
-
好的,让我们调试一下:在
simple_fields_for调用上方添加<%= (f.object && f.object.sub_groups.count).inspect %>。
标签: ruby-on-rails ruby simple-form nested-forms