# Redirect question

**URL:** <https://www.ruby-forum.com/t/redirect-question/121846>\
**Category:** Rails\
**Created:** [December 1, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/redirect-question/121846 "2007-12-01T20:55:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cedarwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cedarwood](https://www.ruby-forum.com/u/cedarwood)\
**Post date:** [December 1, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/redirect-question/121846/1 "2007-12-01T20:55:42Z")

</div>

Hi

I’m hoping someone can help me with the following:

I am trying to use a redirect to pass variables as a post request but  
may be having trouble with the syntax.

redirect\_to(:controller =\> ‘individual’, :action =\> ‘dashboard’, :id  
=\> c.id, :display\_year =\> year, :display\_week =\> week, :name  
=\> :custom, :method =\> :post)

I have been able to achieve a similar effect using a link\_to method  
(as below) - but am unable to achieve the same result using the  
redirect.

\<%= link\_to c.name, {:controller =\> ‘individual’, :action =\>  
‘dashboard’, :id =\> c.id, :display\_year =\> year, :display\_week =\>  
week], :name =\> :custom}, :method =\> :post %\>

Thanks in advance

---

<div class="post-metadata">

**Author:** ![cedarwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cedarwood](https://www.ruby-forum.com/u/cedarwood)\
**Post date:** [December 1, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/redirect-question/121846/2 "2007-12-01T21:50:09Z")

</div>

When you provide :method =\> :post to link\_to it generates javascript to  
post to the url. You can not do the same thing with redirect\_to.

-Bill

---

<div class="post-metadata">

**Author:** ![cedarwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cedarwood](https://www.ruby-forum.com/u/cedarwood)\
**Post date:** [December 2, 2007, 12:28am UTC](https://www.ruby-forum.com/t/redirect-question/121846/3 "2007-12-02T00:28:48Z")

</div>

Is there another way of generating a :post request that would achieve  
the desired effect?

---

<div class="post-metadata">

**Author:** ![cedarwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cedarwood](https://www.ruby-forum.com/u/cedarwood)\
**Post date:** [December 2, 2007, 9:45am UTC](https://www.ruby-forum.com/t/redirect-question/121846/4 "2007-12-02T09:45:40Z")

</div>

I really just need to trick the target controller into thinking that  
it is receiving a post request.

If this is the case would the syntax look like this?

redirect\_to(:controller =\> ‘individual’, :action =\> ‘dashboard’, :id  
=\> c.id, :display\_year =\> year, :display\_week =\> week, :name  
=\> :custom, ‘\_method’ =\> :post)

I also noticed a method called post\_via\_redirect(path, args={}) could  
this be used?

Thanks  
Adrian

---

<div class="post-metadata">

**Author:** ![cedarwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cedarwood](https://www.ruby-forum.com/u/cedarwood)\
**Post date:** [December 2, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/redirect-question/121846/5 "2007-12-02T17:28:14Z")

</div>

That redirect\_to looks like it would do it (if \_method does/still  
works). Post\_via\_redirect is used in integration tests so I don’t really  
know if / how that would work in a standard controller, but I’m fairly  
sure it won’t do what you want. The problem you are running into here is  
not a rails limitation but an http limitation. The only way to redirect  
in HTTP is a 302 and that is performed as a get request. If you want  
post you can fake the controller out or use javascript. Is it possible  
that you are hitting a design problem? Seems to me that if you are  
having to post back to your own application then you may be going the  
long way around. Can you not save the needed info in the session or call  
the wanted method from your current method then render the result?

-Bill

---

<div class="post-metadata">

**Author:** ![cedarwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cedarwood](https://www.ruby-forum.com/u/cedarwood)\
**Post date:** [December 2, 2007, 1:00am UTC](https://www.ruby-forum.com/t/redirect-question/121846/6 "2007-12-02T01:00:23Z")

</div>

Do you actually need a post, or do you just need rails to _think_ it is  
a post? If the latter will work, try passing in ‘\_method’ =\> :post. I  
seem to remember that this is how you can trick controllers into  
thinking it’s a post request but I am not 100% sure. Another option  
would be through rjs / ajax, but I have not done it. I am not sure if  
the browser would consider an automated post a security issue.

-Bill

---

<div class="post-metadata">

**Author:** ![cedarwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cedarwood](https://www.ruby-forum.com/u/cedarwood)\
**Post date:** [December 2, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/redirect-question/121846/7 "2007-12-02T22:56:14Z")

</div>

Why can’t you just pass the variables as parameters to a get request  
instead of a post?

-Bill

---

<div class="post-metadata">

**Author:** ![cedarwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cedarwood](https://www.ruby-forum.com/u/cedarwood)\
**Post date:** [December 2, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/redirect-question/121846/8 "2007-12-02T20:52:04Z")

</div>

Perhaps if I put the problem in context:

The application has a user dashboard that displays orders over a date  
range. The date range can be altered by selecting week and year values  
then pressing a submit button that causes a post request that the  
controller method handles causing the dashboard to display the  
selected week/year data. Once the date range is selected the user can  
then modify the data. Once the changes to the data are complete I was  
trying to return the view to the selected week/year values to provide  
visual confirmation of the changes rather than just returning to the  
default settings which correspond to today’s week/year. Hence the  
problem associated with using a redirect as a post request.

Can you suggest a better design?

Thanks  
Adrian

---

<div class="post-metadata">

**Author:** ![cedarwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cedarwood](https://www.ruby-forum.com/u/cedarwood)\
**Post date:** [December 2, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/redirect-question/121846/9 "2007-12-02T23:34:19Z")

</div>

Yes - probably can - I was really just using the post/get difference  
to separate the default state from the custom state. So I guess I  
could use the parameters to differentiate the states.

Thanks for the advice

Cheers  
Adrian
