Date.new returns the undefined `div 'method for" 11 ": String

I get an error

undefined `div 'method for" 11 ": String"

pointing to the @startdate line when I submit the form. I don’t understand what is going on at all. If I take steps in the rails console, it works fine.

In my controller, I:

 @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) 

The hash that I send is:

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

If i do

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

I get:

12-2-11

Therefore, I do not see a problem. Everything seems to be working fine.

+7
source share
1 answer

You pass Date.new strings when you need to pass integers:

 @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) 

In addition, if you do not need to use these variables in your view, there is no need to make them instance variables, and instead you must make local variables instead (i.e. remove the leading @ in the name).

+11
source

All Articles