Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Conditional redirect in rails app controller

I have an action in a controller that I call from two different views. In each case, I want the action to redirect back to the page on which the link was clicked. At the moment I am doing this...

In this view I am passing a parameter like this...

%a.showtooltip#wprofile{:href => idea_vote_up_path(@idea, :source => 'idea'),  :title => 'Awesome idea - vote up !', }

and in the controller...

if params[:source] == 'idea'
  redirect_to idea
else
  redirect_to ideas_path
end

This works fine, but does not feel elegant, especially as it ends up being in a few actions. Is there a better way?

like image 229
Gareth Burrows Avatar asked Aug 14 '14 18:08

Gareth Burrows


1 Answers

You can rewrite it in following way:

redirect_to params[:source] == 'idea' ? idea : ideas_path

If you want to redirect back to the page (refresh current page)

redirect_to request.referer
like image 114
Bongs Avatar answered Sep 27 '22 21:09

Bongs