# Redirect\_to restful-ful resource create action

**URL:** <https://www.ruby-forum.com/t/redirect-to-restful-ful-resource-create-action/121273>\
**Category:** Rails\
**Created:** [November 26, 2007, 11:46am UTC](https://www.ruby-forum.com/t/redirect-to-restful-ful-resource-create-action/121273 "2007-11-26T11:46:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [November 26, 2007, 11:46am UTC](https://www.ruby-forum.com/t/redirect-to-restful-ful-resource-create-action/121273/1 "2007-11-26T11:46:08Z")

</div>

Hello,

What’s a neat way to redirect\_to the create action of a restful  
resource? Unless the request is POST, you fall through to the index  
action.

For example: a logged out user submits the following a comment form.

\<% form\_for @post, posts\_path(@topic) -%\>  
\<!-- stuff -%\>  
\<% end -%\>

Next request, the authorization filter detects they are not logged in.

# somewhere a method in application.rb

unless @user.logged\_in?  
session[:redirection] = params  
redirect\_to :action =\> ‘login’ and return false  
end

The logged-out user submits the login form, and the next request the  
controller logs them in and attempts to perform the action the user  
was orginally performing - POSTing a comment.

# somewhere in ApplicationController.welcome(user)

redirection = session[:redirection] || home\_url  
redirect\_to redirection # FAILS

# redirection.inspect =\> { :controller =\> ‘posts’, :action =\>

‘create’, :post =\> { :body =\> blah … } }

Because topics and posts are restful resources, the user is redirected  
to the index action of the posts controller posts, not the create  
action of the posts controller. This is because the restful route  
generator is getting a POST request from redirect\_to.

What’s a neat way round this (as opposed to hacking in a before\_filter  
or rewriting my index action)?

Thanks for looking,  
Dave

PS I’m on 1.2.5 btw.
