I have a session controller like
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination' # Not quite right!
render 'new'
end
end
if you notice on successful sign in it does redirect_to user which is the show action in the User controller.
But say instead of that I have to go to the new action of user controller then how do i do that?
redirect_to useris just a shortcut forredirect_to url_for(user), which generates the url for a given resource (url_for) and then redirects to it.If you want to redirect to another url, you can use the path helper.
You could also use the
url_forhelper to generate an url.Which can be shortened to
You can even specify an url directly (
redirect_to '/users/new') – which I would not recommend, as you can’t change your routing later without changing all the urls.The docs
As you can see, there are many ways to specify an url to
redirect_toin rails. Take a look at the documentation for all of them