Can't pass parameters using redirect_to


#1

I’m unable to pass a subset of a form’s parameters to another method
within the same controller. The error message is “wrong number of
arguments (0 for 3)” and there aren’t any parameters listed.

Here’s the controller code:
class InfoController < ApplicationController
def show_actual_forecast_data
# parameters from the form
data_type = params[:datatype]
productnbr = params[:productlinenbr]
start_date = params[:start_date]
end_date = params[:end_date]
detail_or_sum = params[:detail_sum]

@time_search = TimePeriodSearch.new(data_type, productnbr,

start_date, end_date, dtail_or_sum)

  if detail_or_sum == "detail" then
    if dtype == "actual" then
    redirect_to({:action => 'test_page'}, :datatype => data_type,
             :start_date => start_date,
             :end_date => end_date)
    end
  end

end

def test_page(datatype, start_date, end_date)
@actual_data = Dailytotal.actual_data_sum(datatype, start_date,
end_date)
end
end

Here’s the TimePeriodSearch model:

class TimePeriodSearch

attr_accessor :datatype, :productnbr, :start_date, :end_date,
:detail_or_sum

def initialize(datatype, productnbr, start_date, end_date,
detail_or_sum)
@datatype, @productnbr = datatype, productnbr
@start_date, @end_date = start_date, end_date
@detail_or_sum = detail_or_sum
end

end

Any feedback would be greatly appreciated.


#2

On Apr 20, 8:33 am, Paul B. removed_email_address@domain.invalid
wrote:

I’m unable to pass a subset of a form’s parameters to another method
within the same controller. The error message is “wrong number of
arguments (0 for 3)” and there aren’t any parameters listed.

def test_page(datatype, start_date, end_date)
@actual_data = Dailytotal.actual_data_sum(datatype, start_date,
end_date)
end
end

If test_page is an action, then it cannot have any parameters unless
those parameters have default values.

Either:
def test_page
@actual_data = Dailytotal.actual_data_sum(params[:datatype],
params[:start_date], params[:end_date])
end

Or:
def test_page(datatype = params[:datatype], start_date =
params[:start_date], end_date = params[:end_date])
@actual_data = Dailytotal.actual_data_sum(datatype, start_date,
end_date)
end

should work.

Dan M.
http://www.dcmanges.com/blog


#3

Uh… redirect_to does not call another action… It does an http
redirect… meaning new request… meaning no args are passed.

If you want to call another action in the same controller, just call
it… like any other method.

b


#4

Ben M. wrote:

Uh… redirect_to does not call another action… It does an http
redirect… meaning new request… meaning no args are passed.

If you want to call another action in the same controller, just call
it… like any other method.

b

Dan and Ben–thanks for your insightful feedback!

It’s possible to pass parameters using redirect_to to another method in
the same controller. Here’s what I did.

  1. Fix the redirect_to code.

redirect_to :action => “test_page”, :aproductnbr => productnbr,
:a_start_date => start_date,
:a_end_date => end_date

  1. Change the test_page method to the following
    def test_page
    @actual_data = Dailytotal.actual_data_sum(params[:aproductnbr],
    params[:a_start_date],
    params[:a_end_date])
    end

  2. Ignore the TimePeriodSearch model and @time_search, as they aren’t
    needed.

Cheers,
Paul