Redirect question


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
=>, :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

<%= link_to, {:controller => ‘individual’, :action =>
‘dashboard’, :id =>, :display_year => year, :display_week =>
week], :name => :custom}, :method => :post %>

Thanks in advance

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.


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

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
=>, :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?


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?


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.


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


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?


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


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs