我收到了错误
undefined method `div’ for “11”:String”
提交表单时指向@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
所以我没有看到问题.一切似乎都运转正常.
解决方法
当您需要传递整数时,您将字符串传递给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)