8

我收到错误

"11":String" 的未定义方法 `div'

@startdate提交表单时指向该行。我完全不明白发生了什么事。如果我在 rails 控制台中执行这些步骤,它就可以正常工作。

在我的控制器中,我有:

@startday = params["startday_#{i}".to_sym]
@startmonth = params["startmonth_#{i}".to_sym]
@startyear = params["startyear_#{i}".to_sym].to_s
@endday = params["endday_#{i}".to_sym]
@endmonth = params["endmonth_#{i}".to_sym]
@endyear = params["endyear_#{i}".to_sym].to_s
@startdate = params["startdate_#{i}".to_sym]
@price = params["price_#{i}".to_sym]
@currency = params[:currency]
@startdate = Date.new(@startyear, @startmonth, @startday)
@enddate = Date.new(@endyear, @endmonth, @endday)

我发送的哈希是:

{
  "startmonth_1"=>"2",
  "startday_1"=>"11",
  "startyear_1"=>"12",
  "endmonth_1"=>"2",
  "endday_1"=>"13",
  "endyear_1"=>"12",
  "price_1"=>"12",
}

如果我做一个

@startee = @startyear.to_s + '-' + @startmonth.to_s + '-' + @startday
return render :text => @startee

我得到:

12-2-11

所以我看不出问题。一切似乎都正常。

4

1 回答 1

14

Date.new当您需要传递整数时,您将传递字符串:

@startday = params[:"startday_#{i}"]
@startmonth = params[:"startmonth_#{i}"]
@startyear = params[:"startyear_#{i}"].to_s
@endday = params[:"endday_#{i}"]
@endmonth = params[:"endmonth_#{i}"]
@endyear = params[:"endyear_#{i}"].to_s
@startdate = params[:"startdate_#{i}"]
@price = params[:"price_#{i}"]
@currency = params[:currency]

@startdate = Date.new(@startyear.to_i, @startmonth.to_i, @startday.to_i)
@enddate = Date.new(@endyear.to_i, @endmonth.to_i, @endday.to_i)

此外,如果您不需要在视图中使用这些变量,则无需将它们设为实例变量,而应将它们设为局部变量(即去掉@名称中的前导)。

于 2012-09-19T01:01:39.687 回答