Forum: Ruby on Rails can't pass parameters using redirect_to

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
98b09438b81a691335acddf6a7c7efca?d=identicon&s=25 Paul Bednar (pbednar)
on 2007-04-20 14:33
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.
B0f09f622c913a13b9bfd332fca56563?d=identicon&s=25 Dan Manges (dcmanges)
on 2007-04-22 06:36
(Received via mailing list)
On Apr 20, 8:33 am, Paul Bednar <rails-mailing-l...@andreas-s.net>
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 Manges
http://www.dcmanges.com/blog
8310c5a7c769345114597bcdef111488?d=identicon&s=25 Ben Munat (Guest)
on 2007-04-22 08:35
(Received via mailing list)
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
98b09438b81a691335acddf6a7c7efca?d=identicon&s=25 Paul Bednar (pbednar)
on 2007-04-25 21:55
Ben Munat 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

2. 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

3. Ignore the TimePeriodSearch model and @time_search, as they aren't
needed.

Cheers,
Paul
This topic is locked and can not be replied to.