# Can't pass parameters using redirect\_to

**URL:** <https://www.ruby-forum.com/t/cant-pass-parameters-using-redirect-to/92540>\
**Category:** Rails\
**Created:** [April 20, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/cant-pass-parameters-using-redirect-to/92540 "2007-04-20T14:33:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [April 20, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/cant-pass-parameters-using-redirect-to/92540/1 "2007-04-20T14:33:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [April 22, 2007, 6:36am UTC](https://www.ruby-forum.com/t/cant-pass-parameters-using-redirect-to/92540/2 "2007-04-22T06:36:25Z")

</div>

On Apr 20, 8:33 am, Paul B. [removed\_email\_address@domain.invalid](mailto: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](http://www.dcmanges.com/blog)

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [April 22, 2007, 8:35am UTC](https://www.ruby-forum.com/t/cant-pass-parameters-using-redirect-to/92540/3 "2007-04-22T08:35:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [April 25, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/cant-pass-parameters-using-redirect-to/92540/4 "2007-04-25T21:55:05Z")

</div>

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
